Palindrome Number leetcode java
题目:
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.
题解:
最开始是用Reverse Integer的方法做的,通过了OJ,不过那个并没有考虑越界溢出问题。遇见这种处理Number和Integer的问题,首要考虑的就是会不会溢出越界。然后再考虑下负数,0。还有就是要心里熟悉\的结果和%的结果。感觉这种题就是考察你对数字敏感不敏感(反正我是很不敏感)
有缺陷的代码如下:
1 public boolean isPalindrome(int x) {
2 if(x<0)
3 return false;
4
5 int count = 0;
6 int testx = x;
7 while(testx!=0){
8 int r = testx%10;
9 testx = (testx-r)/10;
count++;
}
int newx = x;
int result = 0;
while(newx!=0){
int r = newx%10;
int carry = 1;
int times = count;
while(times>1){
carry = carry*10;
times--;
}
result = result+r*carry;
newx= (newx-r)/10;
count--;
}
return result==x;
}
另外一种方法可以避免造成溢出,就是直接安装PalidromeString的方法,就直接判断第一个和最后一个,循环往复。这样就不会对数字进行修改,而只是判断而已。
代码如下:
1 public boolean isPalindrome(int x) {
2 //negative numbers are not palindrome
3 if (x < 0)
4 return false;
5
6 // initialize how many zeros
7 int div = 1;
8 while (x / div >= 10) {
9 div *= 10;
}
while (x != 0) {
int left = x / div;
int right = x % 10;
if (left != right)
return false;
x = (x % div) / 10;
div /= 100;
}
return true;
}
Reference:
http://www.programcreek.com/2013/02/leetcode-palindrome-number-java/
Palindrome Number leetcode java的更多相关文章
- Palindrome Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Palindrome Number - LeetCode 注意点 负数肯定是要return false的 数字的位数要分奇数和偶数两种情况 解法 解法一: ...
- leetcode 第九题 Palindrome Number(java)
Palindrome Number time=434ms 负数不是回文数 public class Solution { public boolean isPalindrome(int x) { in ...
- Letter Combinations of a Phone Number leetcode java
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- Palindrome Number ---- LeetCode 009
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- Ugly Number leetcode java
问题描述: Write a program to check whether a given number is an ugly number. Ugly numbers are positive n ...
- Single Number leetcode java
问题描述: Given an array of integers, every element appears twice except for one. Find that single one. ...
- Palindrome Partitioning leetcode java
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- Valid Number leetcode java
题目: Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...
- 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...
随机推荐
- Bzoj4237 cdq分治+树状数组+单调栈
二维平面在某区域内点的问题,要么树套树,kdtree,要么就是cdq分治了.然而这题树套树和kdtree都不是很好搞的样子,于是我们就只能cdq分治了.首先把点按照横坐标x排序,在每一层我们需要算出右 ...
- BZOJ.4180.字符串计数(后缀自动机 二分 矩阵快速幂/倍增Floyd)
题目链接 先考虑 假设S确定,使构造S操作次数最小的方案应是:对T建SAM,S在SAM上匹配,如果有S的转移就转移,否则操作数++,回到根节点继续匹配S.即每次操作一定是一次极大匹配. 简单证明:假设 ...
- BZOJ2915 : [Poi1997] gen
设f[i][j]表示串ij可以由哪些字母成长过来,用二进制压位表示. 设g[i][j]表示给定串中[i,j]这个区间一开始可以由哪些字母成长多来,用二进制压位表示. 设h[i]表示给定串前i位最少需要 ...
- 关于输出螺旋矩阵的demo
输出类似 1 2 3 8 9 4 7 6 5 主要难点是如何找到表示的算法 我的理解是,先生成一个n*n的矩阵,然后再往里面塞数字,而塞的方法分别有四种:由左往右,由上往下,由右往左,由下往上,没塞完 ...
- hdu 5752 Sqrt Bo 水题
Sqrt Bo 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5752 Description Let's define the function f ...
- ROS知识(20)----SLAM资源集合
1.各种最新开源的SLAM a.OpenSLAM.这里收集了各种最新的开源SLAM资料,包含了比如: ORB_SLAM, ORB_SLAM2, hector_slam,ethzasl_ptam,g2o ...
- android adb命令 unable to connect to 192.168.1.155:5555
如果使用有线网络无法用adb connect命令连接设备的话,可以选择使用无线wifi来连接. 首先在android设备上装一个叫做Adb Wireless的软件,打开wifi,然后打开adb wir ...
- Android内存机制分析——堆和栈
昨天用Gallery做了一个图片浏览选择开机画面的功能,当我加载的图片多了就出现OOM问题.以前也出现过这个问题,那时候并没有深究.这次打算好好分析一下Android的内存机制. 因为我以前是做VC+ ...
- CentOS快速安装最新版本的SaltStack
SaltStack是一个类似Puppet的自动运维管理工具,打算用它来管理实验室的所有服务器包括OpenStack,当然要想把它玩起来首先得把它安装上, 这里我写了一个简单的脚本来在CentOS下快速 ...
- 移植Python2到TQ2440
环境 Python:2.7.13 开发板: TQ2440 工具链: arm-none-linux-gnueabi-gcc 4.8.3 概述 前面已经把Python3移植到TQ2440上面的,现在我们移 ...