码迷,mamicode.com
首页 > 其他好文 > 详细

9. Palindrome Number

时间:2015-04-30 07:36:35      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

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;
  }
}

9. Palindrome Number

标签:

原文地址:http://www.cnblogs.com/shini/p/4467918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!