Leetcode:Palindrome Number
2015-07-04 11:21
281 查看
题目出处:https://leetcode.com/problems/palindrome-number/
题目:Determine whether an integer is a palindrome. Do this without extra space.
翻译:判断一个整型数是否是回文数
思路:将整型数转化为字符串,依次比较首尾
代码:
另外,也看了下大牛的代码,真精炼!出处:http://blog.csdn.net/hcbbt/article/details/44001229
代码如下:
题目:Determine whether an integer is a palindrome. Do this without extra space.
翻译:判断一个整型数是否是回文数
思路:将整型数转化为字符串,依次比较首尾
代码:
<span style="background-color: rgb(204, 255, 255);">public class Solution { public boolean isPalindrome(int x) { boolean ispm = false; if(x<0) ispm = false; String s = x + ""; int len = s.length(); if(len % 2 == 0) { for(int i = 0; i<=(s.length()-1)/2; i++) { if(s.charAt(i) != s.charAt(len-1 - i)) { ispm = false; break; } else ispm = true; } } else { for(int i = 0; i<=s.length()/2; i++) { if(s.charAt(i) != s.charAt(len-1 - i)) { ispm = false; break; } else ispm = true; } } return ispm; } }</span>
另外,也看了下大牛的代码,真精炼!出处:http://blog.csdn.net/hcbbt/article/details/44001229
代码如下:
public class Solution { public boolean isPalindrome(int x) { long xx = x; long new_xx = 0; while (xx > 0) { new_xx = new_xx * 10 + xx % 10; xx /= 10; } return new_xx == x; } }
相关文章推荐
- oracle忘记了sys,system 密码后怎么修改?
- CAS协议 - CAS URIs
- Scramble String
- Nagel算法
- HDU 3395 Special Fish(最大费用流)
- sed的妙用
- map遍历的几种方式
- 序列化和反序列化
- Codeforces Round #310 (Div. 1)B,C,D(set+线段树)
- [CEOI1997]参观洞穴 解题报告
- mako模板调试与使用技巧
- AppDelegate详解
- 阿里巴巴JAVA常考面试题及汇总答案
- centos 6.5 安装mongodb2.6
- [小米]字符串重组
- nim语言
- postgresql教程
- STRTOK函数的用法
- CLLocationManager位置服务对象和地图的简单结合使用
- [转] iOS - LLDB调试命令初探