zoukankan      html  css  js  c++  java
  • LeetCode : Palindrome Number

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

    click to show spoilers.

    Some hints:
    Could negative integers be palindromes? (ie, -1)

    If you are thinking of converting the integer to string, note the restriction of using extra space.

    You could also try reversing an integer. However, if you have solved the problem “Reverse Integer”, you know that the reversed integer might overflow. How would you handle such case?

    解释:求回文数

    class Solution {
    public:
        bool isPalindrome(int x) {
            vector<int>res;
            if(x<0)
               return false;
             if(x==0)
                return true;
            while(x)
            {
                res.push_back(x%10);
                x/=10;
            }
            int i = 0,j = res.size()-1;
            while(i<=j)
            {
                if(res[i]!=res[j])
                    return false;
               ++i;
               --j;
            }
            return true;
        }
    };
  • 相关阅读:
    Python基础Day2
    HDU
    HDU
    BZOJ
    Gym
    UVA
    UVA
    UVA
    UVA
    BZOJ
  • 原文地址:https://www.cnblogs.com/chankeh/p/6850078.html
Copyright © 2011-2022 走看看