首页 > 其他 > 详细

Leetcode:Palindrome Number

时间:2015-07-04 14:07:57      阅读:244      评论:0      收藏:0      [点我收藏+]

题目出处:https://leetcode.com/problems/palindrome-number/

题目: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;
    }
}





版权声明:本文为博主原创文章,未经博主允许不得转载。

Leetcode:Palindrome Number

原文:http://blog.csdn.net/yangyao_iphone/article/details/46754265

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!