zoukankan      html  css  js  c++  java
  • 9. Palindrome Number

    description:

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

    Example 1:
    
    Input: 121
    Output: true
    Example 2:
    
    Input: -121
    Output: false
    Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
    Example 3:
    
    Input: 10
    Output: false
    Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
    

    my answer:

    感恩

    class Solution {
    public:
        bool isPalindrome(int x) {
            if(x<0) return false;
            string s;
            while(x!=0){
                int add = x%10;
                s+='0'+add;
                x = x/10;
            }
            int n = s.size();
            for(int i = 0; i < n/2; i++){
                if(s[i]!=s[n-1-i]) return false;
            }
            return true;
        }
    };
    

    大佬的answer:

    class Solution {
    public:
        bool isPalindrome(int x) {
            if(x<0||(x%10==0&&x!=0))return false;
            return reverse(x)==x;
        }
        int reverse(int x){
            int res = 0;
            while(x!=0){
                if(res>INT_MAX/10)return -1;//这里很有意思,溢出好像都是判断INT_MAX/10的?
                res = res*10 + x%10;
                x /= 10;
            }
            return res;
        }
    };
    

    relative point get√:

    emmm, string and integer 交换好像都需要'0' 作为跳板咩...?

    hint :

    res>INT_MAX/10)return -1
    res = res*10 + x%10;
    x /= 10;

  • 相关阅读:
    hdu1915
    2014年9月28日 18:35:01
    洛谷—— P1122 最大子树和
    洛谷——P1103 书本整理
    洛谷—— P2049 魔术棋子
    UVA_1575
    洛谷—— P2424 约数和
    中文乱码问题
    JSP标签
    include指令
  • 原文地址:https://www.cnblogs.com/forPrometheus-jun/p/10576297.html
Copyright © 2011-2022 走看看