本来推断回文串是一件非常easy的事情,仅仅须要反转字符串后在与原字符串相比較就可以。这道题目明白说明不能使用额外的空间。那么使用将其分解连接成字符串的方法便不是可行的。仅仅好採用数学的方式: 每次取最高位和最低位相比較,总的位数能够用一个while先处理出来,循环直至取余和除数相等。

详细见代码:

class Solution {
public:
bool isPalindrome(int x) {
if(x<0) //special due
return false;
if(x<10)
return true;
int curMod=0;
int test=x;
while(test)
{
curMod++;
test/=10;
}
curMod--;// bit num
int left=pow(10,curMod*1.0),right=10;
while(right<=left)
{
if(x%right!=x/left)
return false;
x=x%left,x/=10;
left/=100;
}
return true;
}
};

[leetcode] Palindrome Number(不使用额外空间)的更多相关文章

  1. [LeetCode] Palindrome Number 验证回文数字

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  2. leetcode:Palindrome Number (判断数字是否回文串) 【面试算法题】

    题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...

  3. LeetCode——Palindrome Number

    Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...

  4. [Leetcode]Palindrome Number

    Determine whether an integer is a palindrome. Do this without extra space. 这题貌似解法挺多,直接用简单的把数倒置,没有考虑数 ...

  5. leetcode:Palindrome Number【Python版】

    一次AC 题目要求中有空间限制,因此没有采用字符串由量变向中间逐个对比的方法,而是采用计算翻转之后的数字与x是否相等的方法: class Solution: # @return a boolean d ...

  6. [Leetcode] Palindrome number 判断回文数

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  7. leetcode Palindrome Number python

    class Solution(object): def isPalindrome(self, x): """ :type x: int :rtype: bool &quo ...

  8. [LeetCode]Palindrome Number 推断二进制和十进制是否为回文

    class Solution { public: bool isPalindrome2(int x) {//二进制 int num=1,len=1,t=x>>1; while(t){ nu ...

  9. 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...

随机推荐

  1. 前端之CSS属性相关

    宽和高 width属性可以为元素设置宽度. height属性可以为元素设置高度. 块级标签才能设置宽度,内联标签的宽度由内容来决定. 字体属性 文字字体 font-family可以把多个字体名称作为一 ...

  2. Hadoop学习总结(1)——大数据以及Hadoop相关概念介绍

    一.大数据的基本概念 1.1.什么是大数据 大数据指的就是要处理的数据是TB级别以上的数据.大数据是以TB级别起步的.在计算机当中,存放到硬盘上面的文件都会占用一定的存储空间,例如: 文件占用的存储空 ...

  3. C# 字符串 分割 反转 Base64

    "; //字符串 ToBase64 byte[] bytes = Encoding.Default.GetBytes(pwd); pwd = Convert.ToBase64String(b ...

  4. 怎样在HP-UX中安装bash?

     怎样在HP-UX中安装bash? 注:HPUX全称是Hewlett Packard UniX是惠普9000系列server的Unix操作系统,能够在HP的PA-RISC处理器.Intel的Ita ...

  5. 自己主动化脚本ssh以及telnet发送命令并退出(windows和linux都适用)

    须要安装putty,用到的命令是plink: PuTTY Link: command-line connection utility Unidentified build, Jun 23 2015 1 ...

  6. 翻翻git之---炫酷的自己定义翻滚View TagCloudView

    转载请注明出处:王亟亟的大牛之路 周一好,又到了每周最困的一天.近期都被啮齿类动物搞的累死,废话不多,今天上一个自己定义的ViewGroup实现一个3D球形集合. 效果图: 效果还不错,能够作为短小文 ...

  7. Can’t connect to local MySQL server through socket ‘/tmp/mysql/mysql.sock’解决方法

    原因在于/tmp/mysql/mysql.sock不存在,为/usr/local/mysql/mysql.sock建立一个软连接到/tmp/mysql/mysql.sock即可. ln -s /usr ...

  8. C++中冒号(:)的作用

    摘于:http://blog.csdn.net/zimingjushi/article/details/6549390 (1)表示机构内位域的定义(即该变量占几个bit空间) typedef stru ...

  9. JAVA学习书籍

    1 java编程思想(第四版) 2 head first servlet &jsp

  10. UESTC 30最短路(flyod算法)

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...