Palindrome Number溢出怎么处理?

来源:互联网 发布:java继承是什么意思 编辑:程序博客网 时间:2024/05/16 11:19

1. x == reverse(x)?

2. 算术运算


public class Solution {    // 1. could negative number be Palindrome?    // 2. reverse Integer!    public boolean isPalindrome(int x) {        if (x < 0) return false;        return x == reverseInt(x);            }        public int reverseInt(int x) {        int res = 0;        while(x != 0){            res *= 10;            res += x%10;            x = x/10;        }         return res;    }}

public class Solution {    //法二    public boolean isPalindrome(int x) {       if (x < 0) {           return false;       }              //find out how many digits it has       int div = 1;       while (x/div >= 10) {           div *= 10;       }              while (x != 0) {           int left = x / div;           int right = x % 10;                      if (right != left) {               return false;           }                      x = x % div / 10;           div /= 100;       }       return true;    }}


0 0
原创粉丝点击