【LeetCode】9. Palindrome Number (2 solutions)
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.
解法一:
求出x的逆序数y,判断两者是否相等即可。
时间复杂度:O(k),k为x的位数
class Solution {
public:
int reverse(int x) {
long long y = ;
int num = x; while(num)
{
y = y* + num%;
num /= ;
}
return y; //valid
} bool isPalindrome(int x) {
if(x < )
return false;
if(x == reverse(x))
return true;
else
return false;
}
};
解法二:
转为string类型,然后首尾指针逐字符比较。
时间复杂度:O(k),k为x的位数
class Solution {
public:
bool isPalindrome(int x) {
string xstr = to_string(x);
for(int i=, j=xstr.size()-; i < j; i ++, j --)
{
if(xstr[i] != xstr[j])
return false;
}
return true;
}
};
【LeetCode】9. Palindrome Number (2 solutions)的更多相关文章
- 【LeetCode】9. Palindrome Number 回文数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...
- 【leetcode】9. Palindrome Number
题目描述: Determine whether an integer is a palindrome. Do this without extra space. 解题分析: ^_^个人觉得这道题没有什 ...
- 【leetcode】 9. palindrome number
@requires_authorization @author johnsondu @create_time 2015.7.13 9:48 @url [palindrome-number](https ...
- 【LeetCode】136. Single Number (4 solutions)
Single Number Given an array of integers, every element appears twice except for one. Find that sing ...
- 【LeetCode】009. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- 【LeetCode】9 Palindrome Number 回文数判定
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
随机推荐
- 深度学习研究组Deep Learning Research Groups
Deep Learning Research Groups Some labs and research groups that are actively working on deep learni ...
- C语言:通过函数指针来完成两个数的加减乘除
// // main.c // Function_pointer // // Created by mac on 15/8/2. // Copyright (c) 2015年. All rig ...
- Element table使用技巧详解
1.控制table某些行数不显示 下载附件的需求,有些行有附件,有些没有,所以需要过滤,重点是:Array.filter()使用 <el-card :body-style="{ pad ...
- Android -- 打开本地图片且显示路径
背景 代码 ...
- MongoDB学习笔记(二)--Capped集合 && GridFS存储文件
Capped集合 Capped集合的大小是固定的,如果空间都被用完了,新添加的对象 ...
- Android基础新手教程——1.6 .9(九妹)图片怎么玩
Android基础新手教程--1.6 .9(九妹)图片怎么玩 标签(空格分隔): Android基础新手教程 1.本节引言: 可能有的一些疑问: 1.什么是.9图片? 答:图片后缀名前有.9的图片,如 ...
- 防止excel数字变成科学计数法
在网上查了很多资料知道解决办法大概有两个:一是在身份证字段前加个英文单引号,二是设置Excel的格式为文本格式. 我试用过第一种确实可以显示,但是有个“'”号在那里感觉确实不是很好,虽然听说不影响,但 ...
- Package.json小结
生成package.json 定位到想放置package.json的目录,运行npm init,根据提示就可以生成package.json文件,其中test command可以为空. 安装mo ...
- (转)解决 bitmap size exceeds VM budget (Out Of Memory 内存溢出)的问题
在做图片处理的时候最常遇到的问题估计就是Out Of Memory (内存溢出)了 网上对这种问题的解决方案很多,原来无非就是压缩图片大小 本不该重复造轮子,但实际中却遇见了问题,写出来希望后来者能引 ...
- ZH奶酪:JavaScript清空数组的三种方法
参考链接:http://snandy.iteye.com/blog/893955 方式1,length赋值为0 目前 Prototype中数组的 clear 方法和mootools库中数组的 empt ...