【一天一道LeetCode】#9. Palindrome Number
一天一道LeetCode系列
(一)题目
Determine whether an integer is a palindrome. Do this without extra
space.
Some hints:
Could negative integers be palindromes? (ie, -1)If you are thinking of converting the integer to string, note the
restriction of using extra space.You could also try reversing an integer. However, if you have solved
the problem “Reverse Integer”, you know that the reversed integer
might overflow. How would you handle such case?There is a more generic way of solving this problem.
(二)解题
题目需要判断一个整数是不是回文数。需要注意一下几点:、
1、负数不是回文数
2、回文数满足反转后与原数相等,在反转整数那一题里面提到需要考虑越界问题,所以需要将int转换成long来处理。
代码比较简单,如下:
class Solution {
public:
bool isPalindrome(int x) {
if(x<0) return false;
else{
long n = 0;
long temp = x;//考虑到反过来越界
long lx = x;
while(temp)
{
n = n*10+temp%10;//反转int数
temp = temp/10;
}
if(n==lx) return true; //与原数相等则返回true
else return false;
}
}
};
【一天一道LeetCode】#9. Palindrome Number的更多相关文章
- 【一天一道LeetCode】#191. Number of 1 Bits
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- Leetcode练习题 Palindrome Number
9. Palindrome Number Question: Determine whether an integer is a palindrome. An integer is a palindr ...
- Leetcode 9. Palindrome Number(水)
9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- LeetCode 9. Palindrome Number (回文数字)
Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...
- Leetcode 9. Palindrome Number(判断回文数字)
Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...
- [LeetCode][Python]Palindrome Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/palindr ...
- 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]
题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- [LeetCode] 9.Palindrome Number - Swift
Determine whether an integer is a palindrome. Do this without extra space. 题目意思:判断一个整数是否是回文数 例如:1232 ...
- [LeetCode] 9. Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
随机推荐
- Android开发学习之路--基于vitamio的视频播放器(二)
终于把该忙的事情都忙得差不多了,接下来又可以开始good good study,day day up了.在Android开发学习之路–基于vitamio的视频播放器(一)中,主要讲了播放器的界面的 ...
- 在一维坐标轴上有n个区间段,求重合区间最长的两个区间段。
//重叠区间数 #define N 2 typedef struct arrange { int x; int y; } Arrange; //先按左边界排序,若相等再按右边界排序(升序) int c ...
- Python excel 奇怪的通信规则
直接: for i in range(1,n+1): print(i) if xls.getCell(i,1,1)=='id': res=[] xls.xlBook.Worksheets(i).Nam ...
- 打开Voice Over时,CATextLayer的string对象兼容NSString和NSAttributedString导致的Crash(二解决思路3)
续前一篇:打开Voice Over时,CATextLayer的string对象兼容NSString和NSAttributedString导致的Crash(二解决思路2)ok,到这里已经能够锁定范围了, ...
- Android App之间通过Intent交互
Android 最重要的功能之一是应用能够基于它要执行的"操作"向另一个应用发送用户. 例如,如果您的应用有您要在地图上显示的公司地址,您无需在显示地图的应用中构建 Activit ...
- tomcat自动运行磁盘任意位置上的项目、使用Maven对tomcat进行自动部署
对于非Maven的web项目,有时候我们想不时常通过打war包.拷贝war包.启动tomcat来运行项目.这时候我们可以通过以下方式来进行配置: 1.1:创建web工程.工程结构如下: 1.2. ...
- Spark发展现状与战线
前言 现今Spark正是风头正劲时,Spark本是UCBerkeley的AMPLab诞生的项目,后来捐赠给了Apache来管理源码和后续发展.今年从Apache孵化器终于孵化出了1.0版本.其对大数据 ...
- iOS下JS与OC互相调用(六)--WKWebView + WebViewJavascriptBridge
上一篇文章介绍了UIWebView 如何通过WebViewJavascriptBridge 来实现JS 与OC 的互相调用,这一篇来介绍一下WKWebView 又是如何通过WebViewJavascr ...
- iOS开发之Xcode8推出的WKWebView与UIWebView的使用
一.整体介绍 UIWebView自iOS2就有,WKWebView从iOS8才有,毫无疑问WKWebView将逐步取代笨重的UIWebView.通过简单的测试即可发现UIWebView占用过多内存,且 ...
- Android初级教程:对文件和字符串进行MD5加密工具类
转载请注明出处:http://blog.csdn.net/qq_32059827/article/details/52200008 点击打开链接 之前写过一篇博文,是针对字符串进行md5加密的.今 ...