LeetCode9 Palindrome Number
题意:
Determine whether an integer is a palindrome. Do this without extra space. (Easy)
分析:
自己考虑的方法是利用Reverse Integer,然后查看rev与x是否相等,注意负数和0的判断(这里WA了一次)
代码1:
class Solution {
private:
int reverse(int x) {
long long result = ;
while (x != ) {
int temp = x % ;
x /= ;
result = result * + temp;
if (result > 0x7FFFFFFF || result < -0x7FFFFFFF) {
return ;
}
}
return result;
}
public:
bool isPalindrome(int x) {
if (x < ) {
return false;
}
int rex = reverse(x);
if (rex == && x != ) { //溢出(不是因为等于0而得到rex = 0)
return false;
}
if (rex == x) {
return true;
}
return false;
}
};
查看讨论区,有只比较一半的解法,循环中的思路和reverse integer类似,更为简洁,实现如下;
代码2:
class Solution {
public:
bool isPalindrome(int x) {
if (x < || (x % == && x != ) ) {
return false;
}
int sum = ;
while (x > sum) {
int temp = x % ;
sum = sum * + temp;
x /= ;
}
if (x == sum || x == sum / ) {
return true;
}
return false;
}
};
LeetCode9 Palindrome Number的更多相关文章
- leetcode9 Palindrome Number(按进阶要求)
题目描述 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same ...
- 65. Reverse Integer && Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, re ...
- 有趣的数-回文数(Palindrome number)
文章转自http://blog.163.com/hljmdjlln@126/blog/static/5473620620120412525181/ 做LC上的题"Palindrome num ...
- 9. Palindrome Number
/* Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers ...
- No.009 Palindrome Number
9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...
- 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List
9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...
- leetcode 第九题 Palindrome Number(java)
Palindrome Number time=434ms 负数不是回文数 public class Solution { public boolean isPalindrome(int x) { in ...
- HDU 5062 Beautiful Palindrome Number(数学)
主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5062 Problem Description A positive integer x can re ...
- Reverse Integer - Palindrome Number - 简单模拟
第一个题目是将整数进行反转,这个题实现反转并不难,主要关键点在于如何进行溢出判断.溢出判断再上一篇字符串转整数中已有介绍,本题采用其中的第三种方法,将数字转为字符串,使用字符串比较大小的方法进行比较. ...
随机推荐
- openstack network
- always NetWork Performance measure Tools
1,iperf key feature:Measuring TCP and UDP BandWidth Performance Iperf features; *TCP .Measure bandwi ...
- django1.77+mod_wsgi+python2.79+apache2.24 在阿里云centos部署攻略
心平气和的记录一下今天 踩的坑以防万一 以后还踩 首先我今天的平台是在 阿里云上的一台纯净版的 centos6.5 64位主机上进行的 首先装python2.7 去官网下载python2.7安装包 然 ...
- PhysX
[PhysX] 1.施加力: ))) { //施加一个力,X轴方向力度为1000,Y轴方向力度为1000 addFrceObj.rigidbody.AddForce (, , ); } ))) { / ...
- How to run a terminal inside of vim?
[How to run a terminal inside of vim?] :sh turn vim into shell mode d+trl back to vim 参考:http://stac ...
- spring properties resolve 问题
在stackoverflow上看到一个问题 配置如下: <context:property-placeholder location="/WEB-INF/application-cus ...
- 咏南多层开发框架支持最新的DELPHI 10 SEATTLE
购买了咏南多层开发框架的老用户如有需要提供免费升级. 中间件
- POJ3185(简单BFS,主要做测试使用)
没事做水了一道POJ的简单BFS的题目 这道题的数据范围是20,所以状态总数就是(1<<20) 第一次提交使用STL的queue,并且是在队首判断是否达到终点,达到终点就退出,超时:(其实 ...
- POJ 3172 Scales (01背包暴力)
题意:给定 n 个数,保证下一个数比上一个数和前一个数之和大,然后给定一个背包,问你最多放多少容积. 析:应该是很明显的01背包,但是可惜的是,数组开不出来,那就得考虑暴力,因为数不多,才几十而已,要 ...
- How to check “hasRole” in Java Code with Spring Security?
From:http://stackoverflow.com/questions/3021200/how-to-check-hasrole-in-java-code-with-spring-securi ...