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 ...
随机推荐
- [GraphQL] Filter Data Based on Query Arguments with GraphQL
With GraphQL, every field and nested object can have a set of arguments which can be used to request ...
- 一个简单的java回调函数的实现
回调函数 回调函数涉及的3个函数 登记回调函数 回调函数 响应回调函数 简单的解释 你到一个商店买东西,刚好你要的东西没有货,于是你在店员那里留下了你的电话.过了几天店里有货了,店员就打了你的电话,然 ...
- android Menory 小结
不建议在Activity中使用static 变量,考虑使用Application.当然,static final例外 但Application也不要cache某个Activity使用的View,如果c ...
- Linux 进程间通信 --- 信号通信 --- signal --- signal(SIGINT, my_func); --- 按键驱动异步通知(转)
信号 ( signal ) 机制是 UNIX 系统中最为古老的进程间通信机制,很多条件可以产生一个信号. 信号的产生: 1,当用户按下某些按键时,产生信号. 2,硬件异常产生信号:除数为 0 ,无效 ...
- C++五种迭代器之间的关系
迭代器操作 说明(1)所有迭代器p++ 后置自增迭代器++p ...
- 记一次SmtpClient发送邮件引发的系列问题
前提:公司同事离职,我接手同事负责的项目. 事件:某天公司的分析人员,说软件中的邮件发送功能不能使用,总是提示"邮件发送失败". 本地能够正常发送,发布WCF到服务器IIS上,再调 ...
- LA 5009 (HDU 3714) Error Curves (三分)
Error Curves Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu SubmitStatusPr ...
- tcpdump抓包(转)
Linux 环境下,通常通过 tcpdump 来进行抓包和分析.它是几乎所有 Linux 发行版本预装的数据包抓取和分析工具. tcpdump 工具的获取和安装可以参阅相应操作系统的官方文档,本文不再 ...
- ansible 简单用法
ansible批量复制文件到服务器 ansible -i /etc/ansible/hosts.test test -m copy -a "src=/tmp/test dest=/usr/b ...
- json.Decoder vs json.Unmarshal
128down voteaccepted It really depends on what your input is. If you look at the implementation of t ...