LeetCode Problem 9: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.
注意问题要求O(1)空间复杂度,注意负数不是回文数。
思路1:将输入整数转换成倒序的一个整数,再比较转换前后的两个数是否相等,但是这样需要额外的空间开销
思路2:每次提取头尾两个数,判断它们是否相等,判断后去掉头尾两个数。
class Solution {
public:
bool isPalindrome(int x) {
//negative number
if(x < )
return false;
int len = ;
while(x / len >= )
len *= ;
while(x > ) {
//get the head and tail number
int left = x / len;
int right = x % ;
if(left != right)
return false;
else {
//remove the head and tail number
x = (x % len) / ;
len /= ;
}
}
return true;
}
};
LeetCode Problem 9:Palindrome Number回文数的更多相关文章
- 【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. Some hints: Could neg ...
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- Leetcode 3——Palindrome Number(回文数)
Problem: Determine whether an integer is a palindrome. Do this without extra space. 简单的回文数,大一肯定有要求写过 ...
- [LeetCode]9. Palindrome Number回文数
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
- 【LeetCode】Palindrome Number(回文数)
这道题是LeetCode里的第9道题. 题目说的: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: ...
- Palindrome Number 回文数
判断一个数字是否是回文数,尝试不用其他额外空间. 注意: 负数也有可能成为回文数吗? 如果你想让int转为string,注意不用其他空间这个约束. 你也可以翻转一个int,但是有可能会溢出. ...
- 9. Palindrome Number 回文数的判断
[抄题]: Determine whether an integer is a palindrome. An integer is a palindrome when it reads the sam ...
- [LeetCode] Prime Palindrome 质数回文数
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
随机推荐
- uva 10934 装满水的气球
题意和思路见: http://blog.csdn.net/shuangde800/article/details/11273123 我的想法: 首先问题转化一下 将问题转化成:定义f[i][j] 表示 ...
- HTML5游戏探讨,怎样让微信游戏仅仅能执行在微信中
大致文件布局例如以下.一个html文件.一个loading.js,在loading.js中载入其它须要的js和css. 至于详细的速度的话.建议cdn或者一个域中最多载入6个js文件.在loading ...
- jquery如何判断checkbox(复选框)是否被选中 全选 反选
好长时间没用jq, 之前用的都是ng. 想着随便参考一下,结果被坑.因为这篇文章是09年的,也和当时jq的版本号有关,但是为什么在百度排名第一,百度果然坑人,以后还是google 给出坑人文章的链接 ...
- Rational Performance Tester(RPTv8.6) 在launch Schedule 时一直卡在 29%
solution:Rational Performance Tester(RPTv8.6) 在launch Schedule 时一直卡在 29% 打开Task Manager(Windows serv ...
- python challenge - orc.py
http://www.pythonchallenge.com/pc/def/ocr.html recognize the characters. maybe they are in the book, ...
- 《windows核心编程》 18章 堆
堆的优缺点: 优点:让我们专心解决手头问题,不必理会分配粒度和页边界这类事情. 缺点:分配和释放内存块的速度比其他方式慢,而且也无法对物理存储器的调拨和撤销进行直接控制. 什么是堆: 堆就是一块预订的 ...
- win10下iis绑定局域网ip无效的解决方案
win7不会出现此问题 win10会 win8未测试 问题描述 <binding protocol="http" bindingInformation="*:808 ...
- PHP 命名空间namespace 和 use
慕课网教程: http://www.imooc.com/video/7834 PHP 中命名空间的概念和高级语言(如C#.JAVA)有很大的差异,一度让我混淆甚至怀疑它存在的意义和目的. 今天找时间学 ...
- MapReduce-MulitipleOutputs实现自己定义输出到多个文件夹
输入源数据例子: Source1-0001 Source2-0002 Source1-0003 Source2-0004 Source1-0005 Source2-0006 Source3-0007 ...
- C++语言基础(17)-运算符重载
运算符重载的格式为: 返回值类型 operator 运算符名称 (形参表列){ //TODO: } 一.在类里面实例运行符重载 #include <iostream> using name ...