您的位置:首页 > 其它

[LeetCode] 9 - Palindrome Number

2015-08-28 11:05 190 查看
Determine whether an integer is a palindrome. Do this without extra space.

class Solution {
  public:
    bool isPalindrome(int x) {
    if (x < 0) {
      return false;
    }
    if(x < 10) {
      return true;
    }
    int base0 = 1;
    int base1 = 1;
    int x1 = x;
    while (x1 >= 10) {
      x1 /= 10;
      base1 *= 10;
    }
    while (base1 > base0) {
      int low = (x / base0) % 10;
      int high = (x / base1) % 10;
      if (low != high) {
        return false;
      }
      base0 *= 10;
      base1 /= 10;
    }
    return true;
  }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: