【一天一道LeetCode】#9. Palindrome Number
2024-08-24 21:08:34
一天一道LeetCode系列
(一)题目
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.
(二)解题
题目需要判断一个整数是不是回文数。需要注意一下几点:、
1、负数不是回文数
2、回文数满足反转后与原数相等,在反转整数那一题里面提到需要考虑越界问题,所以需要将int转换成long来处理。
代码比较简单,如下:
class Solution {
public:
bool isPalindrome(int x) {
if(x<0) return false;
else{
long n = 0;
long temp = x;//考虑到反过来越界
long lx = x;
while(temp)
{
n = n*10+temp%10;//反转int数
temp = temp/10;
}
if(n==lx) return true; //与原数相等则返回true
else return false;
}
}
};
最新文章
- HTML 学习笔记 JQuery(DOM 操作)
- jQuery的deferred对象使用详解——实现ajax线性请求数据
- hdu 4274 2012长春赛区网络赛 树形dp ***
- Java语言概述
- Zabbix3.0 自动微信报障
- Ibatis学习总结7--SqlMapClient 执行 SQL 语句
- 用 xampp 在 windows/Linux 下搭建代理服务器
- SQL Server 2012清除连接过的服务器名称历史
- 初学JavaScript(入门一)
- 14.2.5.5 Change Buffer
- COM-TEAM
- Crystal Report 纵向排列,多列格式化
- multipath多路径实验01-构建iscsi模拟环境
- 团队作业4——第一次项目冲刺(Alpha版本) Day1
- 【BZOJ3529】数表(莫比乌斯反演,树状数组)
- springMvc(一)
- 使用labelme制作自己的数据集
- 《Java大学教程》—第7章 类的实现
- Linux更改目录及其子目录、文件的访问权限
- 数据库的连接使用——使用ADO.NET连接数据库