博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode 9 - Palindrome Number
阅读量:7294 次
发布时间:2019-06-30

本文共 1051 字,大约阅读时间需要 3 分钟。

题目

题意

判断一个整数是否是回文数。

Example 1:

Input: 121Output: true

Example 2:

Input: -121Output: falseExplanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: 10Output: falseExplanation: Reads 01 from right to left. Therefore it is not a palindrome.

思路

author's blog ==

还是一道超级水题。只需把这个数字反转,然后将反转后的数字和原数字对比。如果一致,说明是回文数。

需要注意的地方是:在反转过程中,用int可能会溢出,用long long存储反转数字即可解决。

代码

class Solution {public://author's blog == http://www.cnblogs.com/toulanboy/    bool isPalindrome(int x) {        if(x < 0)            return false;        long long reverseInt = 0;        int t = x;        while(t){            reverseInt = reverseInt*10 + t%10;            t /= 10;        }        if(x == reverseInt)            return true;        else            return false;    }};

运行结果

Runtime: 16 ms, faster than 96.66% of C++ online submissions for Palindrome Number.Memory Usage: 8.1 MB, less than 80.92% of C++ online submissions for Palindrome Number.

转载于:https://www.cnblogs.com/toulanboy/p/10876349.html

你可能感兴趣的文章
超级详细找CALL写CALL教程[转]
查看>>
蓝桥杯:基础练习 特殊的数字
查看>>
Cairngorm3中文简介
查看>>
数据结构练手05 关于堆的up策略和down策略实现
查看>>
python-排序算法 冒泡和快速排序
查看>>
JAVA jdbc(数据库连接池)学习笔记(转)
查看>>
c#调用webservices
查看>>
CentOS 网络设置修改
查看>>
删除重复项,保留最大值
查看>>
项目开发中的一些注意事项以及技巧总结
查看>>
JDK环境配置记录
查看>>
模型的深度探究
查看>>
codeforces Round #354 (Div. 2) A
查看>>
自用VS Code 上的Markdown 编辑器css
查看>>
判断字符串中是否含有汉字及其汉字的个数
查看>>
mysql Can't connect to local MySQL server through socket 问题解决
查看>>
linux 技术网站
查看>>
jQuery使用示例详解
查看>>
Swift 数据类型
查看>>
数据结构 queue
查看>>