美文网首页
9. Palindrome Number

9. Palindrome Number

作者: 捂不暖的石头 | 来源:发表于2017-11-03 11:26 被阅读0次

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

    Python

    class Solution(object):
        def isPalindrome(self, x):
            """
            :type x: int
            :rtype: bool
            """
            if x < 0:
                return bool(0)
            x_str = str(x)
            if len(x_str) % 2 == 0:
                if x_str[:len(x_str)/2] == x_str[:len(x_str)/2 - 1:-1]:
                    return bool(1)
                else:
                    return bool(0)
            else:
                if x_str[:len(x_str)/2] == x_str[:len(x_str)/2:-1]:
                    return bool(1)
                else:
                    return bool(0)
    

    Java

    class Solution {
        public boolean isPalindrome(int x) {
            char[] x_char= String.valueOf(x).toCharArray();
            boolean result = true;
            for(int i = 0, j = x_char.length - 1; i <= x_char.length && j >= i; i ++, j--){
                if (x_char[i] != x_char[j]) {
                    result = false;
                    return result;
                }
            }
            return result;
        }
    }
    

    相关文章

      网友评论

          本文标题:9. Palindrome Number

          本文链接:https://www.haomeiwen.com/subject/aqgzpxtx.html