Palindrome Number ---- LeetCode 009
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.
Solution 1: (with extra space)
class Solution
{
public:
bool isPalindrome(int x)
{
if(x == ) return true;
if(x < ) return false; vector<int> v;
bool flag = true; // 若x = 13314, 则v为4 1 3 3 1
while(x != )
{
v.push_back(x % );
x = x / ;
} auto left = v.begin(), right = prev(v.end());
for(; left < right; ++left, --right) // 注意: left < right
{
if(*left != *right)
{
flag = false;
break;
}
}
return flag;
}
};
Solution 2:
class Solution
{
public:
bool isPalindrome(int x)
{
if(x < ) return false;
else if(x == ) return true; bool flag = true; int temp = x, y = ;
while(x != )
{
y = y * + x % ;
x = x / ;
}
if(y != temp) flag = false;
return flag;
}
};
Palindrome Number ---- LeetCode 009的更多相关文章
- Palindrome Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Palindrome Number - LeetCode 注意点 负数肯定是要return false的 数字的位数要分奇数和偶数两种情况 解法 解法一: ...
- Palindrome Number leetcode java
题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoiler ...
- 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...
- leetcode bug & 9. Palindrome Number
leetcode bug & 9. Palindrome Number bug shit bug "use strict"; /** * * @author xgqfrms ...
- No.009 Palindrome Number
9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...
- 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List
9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...
- leetcode 第九题 Palindrome Number(java)
Palindrome Number time=434ms 负数不是回文数 public class Solution { public boolean isPalindrome(int x) { in ...
- leetcode题解 9. Palindrome Number
9. Palindrome Number 题目: Determine whether an integer is a palindrome. Do this without extra space. ...
- LeetCode--No.009 Palindrome Number
9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...
随机推荐
- C#压缩加密和vb压缩加密
string[] FileProperties = new string[2]; FileProperties[0] = "C:\\a\\";//待压缩文件目录 FilePrope ...
- (29)odoo的可用小图标
* 系统的小图标都采用了 fontawesome 官网是 http://fontawesome.dashgame.com/ * 运用小图标 # 首先打开官网 http://fonta ...
- Mybatis 学习-3
1.设计Dao接口 public interface UserDao { public boolean addUser(User user); } public interface CategoryD ...
- inline-block的简单理解
1. 概念display:inline-block 将对象呈现为inline对象,但是对象的内容作为block对象呈现.之后的内联对象会被排列在同一行内.比如我们可以给一个link(a元素)inlin ...
- C/C++深度copy和浅copy
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string. ...
- 11 自定制shell提示符
shell提示符 huiubantu@ubuntu:~$ shell提示符保存在PS1变量中 包括用户名,主机名,当前工作目录 可以通过echo命令查看PS1的内容 huiubantu@ubuntu ...
- 必须关注的25位知名JavaScript开发者
必须关注的25位知名JavaScript开发者 发表于2012-08-07 17:30| 16215次阅读| 来源Crossrider Blog| 46 条评论| 作者Crossrider Blog ...
- jQuery制作瀑布流(转)
“瀑布流布局”随着pinterest网的流行而出名,现在国内使用这种风格布局的网站也越来越多,比如说Mark之,蘑菇街,点点网,哇哦等等.我第一次听到这个布局名称是来自于“乔花写的<瀑布流布局浅 ...
- windows下捕获dump
一般要捕获异常只需要两个函数:SetUnhandledExceptionFilter截获异常:MiniDumpWriteDump写dump文件.但是由于CRT函数可能会在内部调用SetUnh ...
- svn 备份后双机同步热备失效,提示 W200007 target server does not support atomic revision property edits svynsync:E170009
svn 备份后双机同步热备失效,提示 W200007 target server does not support atomic revision property edits; consider u ...