您的位置:首页 > 其它

LeetCode Palindrome Number

2015-06-01 09:36 253 查看
Description:

Determine whether an integer is a palindrome. Do this without extra space.

Solution:

Since it is required that no extra space is used, so we only have to take out the corresponding left and right digit of a number
respectively, and make comparison between them.

public class Solution {
public boolean isPalindrome(int x) {
if (x < 0)
return false;
int bigger = 1, smaller = 1;
for (int i = 1; i < (x + "").length(); i++)
bigger = bigger * 10;
while (bigger >= smaller) {
if (x / bigger % 10 != x / smaller % 10) {
// System.out.println(x / bigger % 10);
return false;
}
bigger = bigger / 10;
smaller = smaller * 10;
}
return true;
}

public static void main(String[] args) {
Solution solution = new Solution();
solution.isPalindrome(12321);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: