zoukankan      html  css  js  c++  java
  • leetcode题解||Palindrome Number问题

    problem:

    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?

    There is a more generic way of solving this problem.


    thinking:


    (1)之前求过int型逆序反转。这里检验一个数是否为一个回文数,能够得到一点启示。

    (2)逆序反转主要涉及溢出问题的处理,并且反转的空间复杂度能够忽略不计,满足题目要求。

    code:

    class Solution {
    public:
        bool isPalindrome(int x) {
            int b=0;
            int a=x;
            bool flag=true;
            if(a<0)
            {
                bool flag=false;
                a=-x;
            }
            while(a)
            {
                b*=10;
                b+=a%10;
                a=a/10;
            }
            if(b>2147483647)
                return false;
            if(!flag)
                b=-b;
            return (b==x);
               
            
        }
    };


  • 相关阅读:
    梦幻如初,心不忘。
    整数集和求并
    ACTF 2014 Write up
    适用web的图片
    jqGrid
    angularjs
    【转载】FPGA异步时钟设计中的同步策略
    Cordic的学习之硬件实现
    Cordic的学习初步
    DDS---三角函数发生器的用法
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/6942059.html
Copyright © 2011-2022 走看看