LeetCode : Palindrome Number [java]

来源:互联网 发布:超级奇门排盘软件 编辑:程序博客网 时间:2024/06/05 07:46

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

思路:依次判断两头数字是否相同,迭代减小数字。


public class Solution {    public boolean isPalindrome(int x) {if (x < 0) {return false;}if (x == 0) {return true;}int base = 1;int record = x;while (x >= 10) {base *= 10;x /= 10;}x = record;while (x > 0) {int left = x / base;int right = x % 10;if (left != right) {return false;}x -= left * base;x /= 10;base /= 100;}return true;    }}


1 0
原创粉丝点击