zoukankan      html  css  js  c++  java
  • 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?

    There is a more generic way of solving this problem.

    思路:这道题判断一个整数是不是回文数,通常做法就是将n模10取余存入数组中,然后除10进入下一循环。然后从这个数组出发,判断头尾是否相同。但是这题强调不使用额外的内存空间,所以应该这样处理,设一变量result,每次循环这样处理result=result*10+base%10;base/=10;注意这题一定要使用一个赋上x的临时变量base,否者我们就会改变x的值,不能进行下面的判断。注意负数不可能是回文数,也就是只针对非负数。

    class Solution {
    public:
        bool isPalindrome(int x) {
            if(x<0)
            {
                return false;
            }
            int result=0;
            int base=x;
            while(base!=0)
            {
                result=result*10+base%10;
                base/=10;
            }
            if(result!=x)
                return false;
            return true;
        }
    };
  • 相关阅读:
    DYCom多平台聊天室Sample
    silverlight多人在线游戏示例
    设计模式之: 命令模式
    Vector
    flashBuilder加入AsDoc注释
    转:每个Flash开发者应该知道的事情
    flashBuilder无法调试:57%就不动了
    UML类图实例
    Dictionary
    Enterprise Architect使用(一)
  • 原文地址:https://www.cnblogs.com/awy-blog/p/3648832.html
Copyright © 2011-2022 走看看