【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: ...
随机推荐
- 从win10家庭版/中文版升级到win10专业版
发布时间:2015-8-4 很多同学在不了解win10系统版本的情况下,安装了win10家庭版/中文版,不管是win10家庭版,还是win10家庭中文版,或者是win10家庭单语言版,它们都是 ...
- opencv2.4中SVD分解的几种调用方法
原帖地址: http://blog.sina.com.cn/s/blog_6109b5d00101ag7a.html 在摄影测量和计算机视觉中,考虑最优解问题时,经常要用到SVD分解.奇异 ...
- 附2 hystrix详述(2)- 配置
一.hystrix在生产中的建议 1.保持timeout的默认值(1000ms),除非需要修改(其实通常会修改) 2.保持threadpool的的线程数为10个,除非需要更多 3.依赖标准的报警和监控 ...
- js list数据 转 树状 层级 JSON,递归生成树状 层级 JSON
<!DOCTYPE html> <html> <head> <script> var data=[ {"id":"aaa& ...
- mongo 3.0 备份和还原数据库 ,及too many positional arguments错误
在mongo 3.0的操作 备份示例 ./mongodump -h localhost -d liongo -o ./ 错误方式: ./mongorestore -h 127.0.0.1 -d lio ...
- 用于Web开发的8 个最好的跨平台编辑器
1) Best Cross Platform IDE - Brackets Brackets是一个在前端Web开发和设计人员中最流行的开放源码IDE/代码编辑器之一.它拥有一些实用工具能够将HTML ...
- 图片文字OCR识别-tesseract-ocr
帮助文件:https://github.com/tesseract-ocr/tesseract/blob/master/doc/tesseract.1.asc 下载地址:https://github. ...
- scala 学习笔记六 推导
1.介绍 在Scala中,推导将生成器.过滤器.和定义组合在一起. 2.例子 有一种将result用作val(而不是var)的方式,:“就地”构建result,而不是逐项构建,利用yield关键字,当 ...
- Angular6
Structural Directives https://angular.io/guide/structural-directives#template-input-variable There a ...
- 解决WordPress 页面无法评论的问题
最近在使用WordPress制作一个企业网站,因为是企业网站所以文章和页面都不需要评论功能,因此在主题里禁用掉了评论功能 //禁用页面和文章的评论功能//add_filter('the_posts', ...