题目:

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

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.

题意判断数字是否是回文串,只能用常数空间,不能转换成字符串,不能反转数字。

先计算数字的位数,分别通过除法和取余,提取出要对比的对应数字。

class Solution {
public:
bool isPalindrome(int x) {
if(x<0)return 0;
int n=0,temp=x,big=1,small=1;
while(temp!=0)
{
n++;
temp/=10;
if(temp)big*=10;
} for(int i=0;i<n/2;++i)
{
if((x/big)%10!=(x/small)%10)return 0;
big/=10;
small*=10;
}
return 1;
}
}; // http://blog.csdn.net/havenoidea

leetcode:Palindrome Number (判断数字是否回文串) 【面试算法题】的更多相关文章

  1. 9 Palindrome Number(判断是否为回文数Easy)

    题目意思:判断是否为回文数,不许使用额外空间 ps:一直不理解额外空间的意思,int能用吗 思路:1.比较头尾 2.翻转,越界问题需考虑 class Solution { public: bool i ...

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

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

  3. 《LeetBook》leetcode题解(5):Longest Palindromic [M]——回文串判断

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  4. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  5. LeetCode(125):验证回文串

    Easy! 题目描述: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, ...

  6. leetcode 每日签到 409. 最长回文串

    题目: 最长回文串 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: ...

  7. 判断最长回文串——暴力、延展、Manacher

    1. 暴力 时间复杂度O(n^3). 2. 延展 以某一字符为中心,设置left, right两个变量同时向外扩,判断他们指向字符是否相同.注意分奇偶讨论.时间复杂度O(n^2). 3. Manach ...

  8. HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)

    题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...

  9. Palindrome - URAL - 1297(求回文串)

    题目大意:RT   分析:后缀数组求回文串,不得不说确实比较麻烦,尤其是再用线段数进行查询,需要注意的细节地方比较多,比赛实用性不高......不过练练手还是可以的.   线段数+后缀数组代码如下: ...

随机推荐

  1. maven第三章 maven使用入门

    3.1编写pom groupId 一个项目名称 artifactId 子项目(模块)名称 version 开发中的版本,稳定的版本 name 项目名称,方便信息交流 http://news.cnblo ...

  2. ASP.NET实现省市区三级联动(局部刷新)

    跟前一篇ASP.NET实现年月日三级联动(局部刷新)一样,没什么技术含量,直接上代码 <asp:ScriptManager ID="ScriptManager1" runat ...

  3. 分享内容到微博、QQ空间、人人网、开心网等社区

    网上有不少分享内容到微博.QQ空间.人人网.开心网等社区的插件,但它们都有自己固定的样式,你不一定会喜欢. 或许你想保持你的网站的原状,添加上微博.QQ空间.人人网.开心网的LOGO图片,点击之后就可 ...

  4. Tomcat:Can't load IA 32-bit .dll on a AMD 64-bit platform问题的解决

    控制台错误如下: java.lang.UnsatisfiedLinkError: D:\apache-tomcat-7.0.56\bin\tcnative-1.dll: Can't load IA 3 ...

  5. UIWebView加载不了页面, 但在电脑的浏览器上可以打开

    经排查, 系因为URL中包含有中文, 所以无法加载页面, 解决方法如下: 将URL进行转码 NSString *urlStr =[[NSString stringWithFormat:@"h ...

  6. 使用CAEmitterLayer实现下雪效果

    效果图: 代码部分: #import "ViewController.h" @interface ViewController () @end @implementation Vi ...

  7. c#0银行存款计算器

    简介: 为银行存款客户提供一个超级计算器,简单直观操作界面,提供一个银行本意到期金额结算查询程序,方便用户选择存款方式. 功能截图: 实验步骤:利用工具栏建造窗体设计如图: 1.建立2个GroupBo ...

  8. error: expected constructor, destructor, or type conversion before '.' token

    今天写代码是遇到这样一个问题error: expected constructor, destructor, or type conversion before '.' token:立马网上查,原来是 ...

  9. C# 将XML格式字符串,写入数据集的表中 XML解析

    将XML格式字符串,写入数据集的表1中   命名空间:using System.Xml;               string strRead;//strRead为以下xml值           ...

  10. CSS代码写出的各种形状图形

    做网页设计时经常要用到各种形状的图形,对于规则的图形很简单,但是对于不规则的图形,一般我们都是用图片,今天就在这里教大家怎样用css代码写出各种规则不同的图形 1.正方形 #square {width ...