Leetcode刷题记——9. Palindrome Number(回文数字)
来源:互联网 发布:淘宝美工兼职招聘 编辑:程序博客网 时间:2024/06/05 05:46
<span style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: transparent;">一、题目叙述:</span>
Determine whether an integer is a palindrome. Do this without extra space.
click to show spoilers.
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.
Subscribe to see which companies asked this question
二、解题思路:
这题挺简单,但我依旧写的非常复杂,代码能力还是很弱,在网上搜了下人家的代码,简洁哭,等会把人家源码也一起贴出来。
1、负数不算是回文
三、源源源码:
1、我的烂码。
public class Solution { public boolean isPalindrome(int x) { boolean isPal = true; int temp = x; int count = 0; if (temp < 0) return false; while (temp != 0) { temp = temp / 10; count ++; } if (x == 0) return true; for (int i = 0; i < count / 2; i++) { temp = x; int left = 1; for (int j = 0; j < i; j++ ) left = left * 10; left = temp / left; left = left % 10; temp = x; int right = 1; for (int j = 0; j < count - 1 - i; j++) right = right * 10; right = temp / right; right = right % 10; if (left != right) {isPal = false; break;} } return isPal; } public static void main(String args[]) { int s = 1; Solution a = new Solution(); System.out.println(a.isPalindrome(s)); }}2、搜的简洁代码
bool isPalindrome(int x) { if (x < 0) return false; int div = 1; while (x / div >= 10) { div *= 10; } while (x != 0) { int l = x / div; int r = x % 10; if (l != r) return false; x = (x % div) / 10; div /= 100; } return true; }
0 0
- Leetcode刷题记——9. Palindrome Number(回文数字)
- LeetCode—Palindrome Number 数字是否是回文数字
- 【LeetCode刷题记录】9. Palindrome Number
- LeetCode 9. Palindrome Number(回文数字)
- LeetCode-9.Palindrome Number(求回文数字)
- [LeetCode] 回文数字 Palindrome Number
- leetCode 9.Palindrome Number (回文数字) 解题思路和方法
- [LeetCode 9] Palindrome Number -- 回文数字
- LeetCode | Palindrome Number(回文数字)
- LeetCode 9 Palindrome Number 回文数字
- LeetCode (30) Palindrome Number (回文数字)
- Palindrome Number(回文数字)
- leetcode——Palindrome Number 判断整数数字是否为回文(AC)
- 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字
- Palindrome Number 判断一个数字是否是回文数字@LeetCode
- 9. Palindrome Number-数字的回文
- 【math】9.Palindrome Number 回文数字判断
- [2017.12.09]9. Palindrome Number 回文数字
- 进程间传递文件描述符--sendmsg,recvmsg
- windows server 2008 r2如何找到自带的磁盘清理工具
- Javascript刷新页面(记录)
- AndroidStudio中如何导入工程
- 使用Jquery X-Menu 插件实现多选checkbox控件的功能
- Leetcode刷题记——9. Palindrome Number(回文数字)
- [QT开发]微信公众号修改菜单工具
- mongodb 分片小计
- 一次android中服务无法停止的原因
- 软引用和弱引用在安卓开发中的使用
- ECMAScript 6 学习笔记----Symbol,Proxy和Reflect
- Window系统64位下的x86文件夹
- JQ的一个树形显示数据插件
- Android操作过程生成Gif动态图