本来推断回文串是一件非常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. WHU 1542 Countries (floyd)

    题意: 在小明出生的星球X上有n国家. 一些国家通过结盟而拥有良好的双边关系,因此他们的公民得益于这些政策,使得所有这些国家之间的旅行变得免费. 但是,不同联盟之间的旅行就不是这么容易了.如果可能,它 ...

  2. 题解 BZOJ4919 【大根堆】

    题面:传送门. 老师说今天要考一道线段树合并,然后...然后这道题我就GG了.(当然可以用线段树合并写,只是比较复杂) 有人赛时想了个贪心,然后被机房巨佬hack了,结果在hack的过程中巨佬想出了正 ...

  3. ArcGIS api for javascript——地理处理任务-瓶中信

    描述 如果在海洋中丢下一个瓶子,本例使用颗粒追踪模型显示指定的天数后瓶子在的地方.首先,输入一个追踪瓶子的天数.然后单击按钮并在海洋里的任意地方画一个点来开始模型.几秒以后将看到一条线出现描述瓶子将去 ...

  4. 百度API调用实例

    今天依据需求要从百度API中取出一些数据.这些操作包含:将坐标转换成百度坐标.依据转换的百度坐标进行特定的查询. 有需求的收藏下,免得下次手写浪费时间. 涉及到的操作有:JSON格式的字符解析.HTT ...

  5. Codeforces Round #313 C. Gerald&#39;s Hexagon(放三角形)

    C. Gerald's Hexagon time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. WireGuard 隧道的安装和使用,测试地址 ca.6tu.me

    WireGuard 端到端平等的网络隧道,测试地址 ca.6tu.me --------------------------------------------- 服务器:远程 Ubuntu18 , ...

  7. 英语影视台词---二、Inception

    英语影视台词---二.Inception 一.总结 一句话总结:盗梦空间 1.You're waiting for a train..A train that will take you far aw ...

  8. HDU 5370 Tree Maker

    一个显然的结论是,一棵n个结点的二叉树的形态数,是Catalan数第n项.

  9. apidoc接口文档的快速生成

    官方文档连接:http://apidocjs.com/#demo apidoc是一个轻量级的在线REST接口文档生成系统,支持多种主流语言,包括Java.C.C#.PHP和Javascript等.使用 ...

  10. "中国制造2025"+"互联网+",引领制造业发展

    "中国制造2025"+"互联网+",引领制造业发展