9. Palindrome Number

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

类似于reverse integer。

public class Solution {
  public boolean isPalindrome(int x) {
    if (x < 0) {
      return false;
    }
    int scale = 1;
    while (x / scale >= 10) {
      scale *= 10;
    }

    while(x > 0) {
      int l = x / scale;
      int r = x % 10;
      if (l != r) {
        return false;
      }
      x = x % scale / 10;
      scale /= 100;
    }
    return true;
  }
}

posted on 2015-04-30 06:21  shini  阅读(102)  评论(0编辑  收藏  举报

导航