Leetcode #9 Easy <Palindrome Number>

题目如图,下面是我的解决方法:
class Solution {
public boolean isPalindrome(int x) {
if(x < 0) //由题意可知,小于0的数不可能为回数, 因此直接返回false
4 return false;
else {
String len = x + "";
int[] arr = new int[len.length()]; //创建数组arr, 长度为x的位数
int result = 0;
int q = x;
for(int i = 0; i < len.length(); i++) //数组从index 0 开始依次存放个位数,十位数,百位数... 然后X依次/10降位
{ arr[i] = x % 10;
x = x/10;
}
for(int i = len.length()-1, j = 0; i>-1; i--,j++) //将储存的数字从数组的 index max 开始依次取出,乘以10^(对应循环次数),即可实现反转
{
result += arr[i] * (int)Math.pow(10,j);
}
if(result == q) //比较反转结果和原来的数
return true;
else
return false;
}
}
}
在看了大神的解题思路,以及对比了大神的解题代码之后,我总结了自己思路以及代码上的缺陷:
1.解题思路太过于耿直,没有思考如何写出优雅的解题算法
2.没有考虑到反转整数可能存在的内存溢出情况
下面是官方解题的代码:
class Solution {
public boolean isPalindrome(int x) {
// 特殊情况:
// 如上所述,当 x < 0 时,x 不是回文数。
// 同样地,如果数字的最后一位是 0,为了使该数字为回文,
// 则其第一位数字也应该是 0
// 只有 0 满足这一属性
if(x < 0 || (x % 10 == 0 && x != 0)) {
return false;
}
int revertedNumber = 0;
while(x > revertedNumber) {
revertedNumber = revertedNumber * 10 + x % 10;
x /= 10;
}
// 当数字长度为奇数时,我们可以通过 revertedNumber/10 去除处于中位的数字。
// 例如,当输入为 12321 时,在 while 循环的末尾我们可以得到 x = 12,revertedNumber = 123,
// 由于处于中位的数字不影响回文(它总是与自己相等),所以我们可以简单地将其去除。
return x == revertedNumber || x == revertedNumber/10;
}
}
作者:LeetCode 链接:https://leetcode-cn.com/problems/palindrome-number/solution/hui-wen-shu-by-leetcode/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
官方解题的思路为:
既然可能存在溢出的情况,那我们就想办法只反转原数的一半来进行比较,这样就不可能出现溢出的情况了!
那么,怎么样知道循环到什么时候已经反转了一半了呢?
很简单,当x <= revertedNumber的时候,即为反转了一半。(注意:这个地方理应只考虑回数反转一半所需的条件!偶回数反转循环结束后只可能x = rN,奇回数反转循环结束后只可能x < rN,然后最终交由return里的条件判断是否为回数即可。形如4321这样的非回偶数,反转循环会出现 43>21的情况,最终得到 x=4,revertedNumber=123。无需纠结因为他不是回数)
Leetcode #9 Easy <Palindrome Number>的更多相关文章
- leetcode bug & 9. Palindrome Number
leetcode bug & 9. Palindrome Number bug shit bug "use strict"; /** * * @author xgqfrms ...
- 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...
- leetcode 第九题 Palindrome Number(java)
Palindrome Number time=434ms 负数不是回文数 public class Solution { public boolean isPalindrome(int x) { in ...
- leetcode题解 9. Palindrome Number
9. Palindrome Number 题目: Determine whether an integer is a palindrome. Do this without extra space. ...
- 【LeetCode】9. Palindrome Number (2 solutions)
Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. click t ...
- [LeetCode 题解]:Palindrome Number
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Determine ...
- LeetCode(9) - Palindrome Number
题目要求判断一个整数是不是回文数,假设输入是1234321,就返回true,输入的是123421,就返回false.题目要求in-place,思路其实很简单,在LeetCode(7)里面我们刚好做了r ...
- 【一天一道LeetCode】#9. Palindrome Number
一天一道LeetCode系列 (一)题目 Determine whether an integer is a palindrome. Do this without extra space. Some ...
- 【算法】LeetCode算法题-Palindrome Number
这是悦乐书的第144次更新,第146篇原创 今天这道题和回文有关,即从前往后和从后往前是一样的,如"上海自来水来自海上"就是一个回文字符串,如整数121就是回文数,这些都是和回文相 ...
随机推荐
- Oracle之:Function :dateToNumber()
create or replace function dateToNumber(i_date in date) return number is result number ; begin resul ...
- 洛谷-P2292-L语言(字典树)
链接: https://www.luogu.org/problem/P2292 题意: 标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章T是 ...
- 2019CCPC秦皇岛赛区(重现赛)- F
链接: http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1006&cid=872 题意: Z 国近年来一直在考虑遏制国土沙 ...
- mysql优化(下)
优化SQL语句:(1)不要使用 select *(2)尽量在where字段上添加索引:(3)模糊查询中%前置不能使用索引,比如 like ‘%a’;(4)使用or语句时,两侧语句都有索引时才使 ...
- 「SNOI2017」礼物
题目链接:Click here Solution: 设\(f(x)\)代表第\(x\)个人送的礼物的数量,\(s(x)\)代表\(f(x)\)的前缀和,即: \[ f(x)=s(x-1)+x^k\\ ...
- 【原创】LUOGU P1808 单词分类
STL大法好!!! 使用sort()将string排序,map去重并统计即可. 最短代码如下: #include<bits/stdc++.h> using namespace std; s ...
- CodeForces–830A--二分,贪心
Office Keys time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- POJ1990--POJ 1990 MooFest(树状数组)
Time Limit: 1000MSMemory Limit: 30000K Total Submissions: 8141Accepted: 3674 Description Every year, ...
- AcWing:145. 超市(贪心 + 小根堆 or 贪心 + 并查集)
超市里有N件商品,每个商品都有利润pipi和过期时间didi,每天只能卖一件商品,过期商品(即当天di<=0di<=0)不能再卖. 求合理安排每天卖的商品的情况下,可以得到的最大收益是多少 ...
- 关于java中对list集合中的数据按照某一个属性进行分组
有的时候,我们需要在java中对集合中的数据进行分组运算. 例如:Bill对象有money(float)和type(String)属性,现有个集合List<Bill>,需要按照Bill的t ...