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 分析:后缀数组求回文串,不得不说确实比较麻烦,尤其是再用线段数进行查询,需要注意的细节地方比较多,比赛实用性不高......不过练练手还是可以的. 线段数+后缀数组代码如下: ...
随机推荐
- JVM 内存分为四大块
1.栈区 存放函数参数值和局部变量值 2.堆区 3.静态区 4.代码区
- Hyper-V的三种网卡
External ======= 虚拟机和物理网络.本地主机都能通信 Internal ======= 虚拟机之间互相通信,并且虚拟机能和本机通信 Private ======= 仅允许运行在这台物理 ...
- office2010删除多余空行
选择 ctrl+H,弹出 "查找和替换"对话框,在"查找内容"输入"^p^p",并在"替换为"输入"^p&qu ...
- Oracle数据库之序列
Oracle数据库之序列(sequence) 序列是一个计数器,它并不会与特定的表关联.我们可以通过创建Oracle序列和触发器实现表的主键自增.序列的用途一般用来填充主键和计数. 一.创建序列 语法 ...
- How To Learn English Very Fast
How do you learn English very fast? Every week, I get emails about this topic. Typically, someone ...
- regular expression (php函数)
1. 正则表达式是一种字符串搜索和匹配的工具 2. php中常用正则表达式函数 preg_match($pattern, $subject) preg_match_all($pattern, $sub ...
- 1105PHP笔记001
关于抽象类:abstract class Car{ abstract function getMaximumSpeed();}class FastCar extends Car{ function g ...
- acdream暴力专场中的优美暴力
F - 小晴天老师系列——苹果大丰收 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Other ...
- Git 初始化配置
先给大家推荐个很不错的GIT学习资料:廖雪峰 <Git简介> http://www.liaoxuefeng.com/wiki/0013739516305929606dd183612485 ...
- 汇编下的i++与++i
故事背景,一个正在c语言的家伙,问我++i 和 i++的问题,我当时因为要去上课没给他说,正好今晚有空就测试了一下如下代码: 编译环境:VS2010 语言:C++ #include <iost ...