leetcode:Palindrome Number (判断数字是否回文串) 【面试算法题】
题目:
Determine whether an integer is a palindrome. Do this without extra space.
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 (判断数字是否回文串) 【面试算法题】的更多相关文章
- 9 Palindrome Number(判断是否为回文数Easy)
题目意思:判断是否为回文数,不许使用额外空间 ps:一直不理解额外空间的意思,int能用吗 思路:1.比较头尾 2.翻转,越界问题需考虑 class Solution { public: bool i ...
- [Leetcode] Palindrome number 判断回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- 《LeetBook》leetcode题解(5):Longest Palindromic [M]——回文串判断
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- LeetCode(125):验证回文串
Easy! 题目描述: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, ...
- leetcode 每日签到 409. 最长回文串
题目: 最长回文串 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: ...
- 判断最长回文串——暴力、延展、Manacher
1. 暴力 时间复杂度O(n^3). 2. 延展 以某一字符为中心,设置left, right两个变量同时向外扩,判断他们指向字符是否相同.注意分奇偶讨论.时间复杂度O(n^2). 3. Manach ...
- HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)
题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...
- Palindrome - URAL - 1297(求回文串)
题目大意:RT 分析:后缀数组求回文串,不得不说确实比较麻烦,尤其是再用线段数进行查询,需要注意的细节地方比较多,比赛实用性不高......不过练练手还是可以的. 线段数+后缀数组代码如下: ...
随机推荐
- hdu 2186
#include <iostream> using namespace std; int main() { int a,b,c,k1,k2,k3,m,n; cin>>m; wh ...
- 各种乱码,编码问题设置方法整理(UTF-8)
一.tomcat中文乱码问题 打开tomcat安装目录,在conf文件夹中找到server.xml文件 ,找到 <Connector port="8009" protoc ...
- vmware桥接模式创建ubuntu虚拟机
- 反序列化 DateTime对象问题
今天在Android的Json反序列化过程中,Date类型无法转化成自己想要的格式,鉴于之前在C#的反序列话中也遇到过这个问题,解决的同时,顺手做个总结,供自己及需要的人日后查阅. 将 ...
- mysql学习(用户权限管理)
1. 添加数据库用户 create user 'username'@'host' identified by 'password'; 提示: 如果想让该用户可以从其他主机登陆,host可以设置为'%' ...
- Css3做的旋转显示文字和角度的变化
Css: .spinner{ width:245px; height:245px; position:relative;}.coly{ border-radius:130px; font-size:1 ...
- PHP判断文章里是否有图片
用preg_match来检查内容里是否有匹配的“<img”,其实我们还用preg_match来判断很多东西,比如邮箱地址里是否有“@”等,下面用一小段代码来演示具体用法. $content=&q ...
- C# 实现将PDF转文本的功能
这篇文章最初只描述使用 PDFBox 来解析PDF文件.现在它已经被扩展到包括使用 IFilter 和 iTextSharp 的例程了. 这篇文章和对应的Visual Studio项目已经更新到目前 ...
- 复杂事件处理引擎—Esper入门
说明: 以下内容,可以参考Esper官方网站<Qucik start & Tutorial >(顺序做了部分调整). PS:因为英语水平有限(大学期间刚过CET4的英语小盲童一枚) ...
- XP系统显示文件夹选项属性被删除解决注册表
Title:XP系统显示文件夹选项属性被删除解决注册表 -- 2010-11-18 13:17 昨天中了个毒窝,还有恶意软件,十分不爽. 开始恶意软件把显示所有文件夹给删除了,去找了个注册表,重新注册 ...