S2
S2 copied to clipboard
0009. Palindrome Number | LeetCode Cookbook
https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0009.Palindrome-Number/
follow up 里提到了别用整数转字符串的方式解决,代码里还是转换了下。这里有点疑惑
follow up 里提到了别用整数转字符串的方式解决,代码里还是转换了下。这里有点疑惑
@gaogao1030 我的锅。。我更新啦。把数字转成数组,然后再首尾判断是否相等。你刷新一下看看更新以后的解法。
follow up 里提到了别用整数转字符串的方式解决,代码里还是转换了下。这里有点疑惑
@gaogao1030 另外我怀疑这个 follow up 是后来加上去的。。我开始做这道题的时候并没有这个要求。
简单题才能让我有一些存在感。。。。。(滑稽)
网上看到另外一个解题思路, 这样能少用一些额外的空间 是把数组翻转,和原本的做对比 java版本的 int reminder = 0; int reverse_value = 0; int num = x; if(x < 0){ return false; } while( num!=0 ){
reminder = num%10; // get the last digit part
// to restore the value , each time we need to take the 2nd part * 10 since it is reminder followed the 1st part
reverse_value = reverse_value * 10 + reminder; // the restore the value
num = num /10;
}
return reverse_value == x;
解法二中 if x< 10 应该return false吧
刷完这个题目,我感觉我又行了