Leetcode(9)之回文数
2020-01-04 18:11
1386 查看
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
题目描述:
解题思路:
只要反转后面一半的位数,和前面剩下的一半的数(向上取整)比,如果一样就是回文数。如果这个数是奇数位,比如12321,我们翻转最后3位就是123,前面剩下的两位是12,那么只要确定 123 / 10 == 12 就行了。
注意特殊情况,负数都不可能是回文数,能被10整除且不是0的不可能是回文数
代码:
public bool IsPalindrome(int x) { if (x < 0) return false; if (x % 10 == 0 && x != 0) return false; int revertedNum = 0; while (x > revertedNum) { revertedNum = 10 * revertedNum + x % 10; x /= 10; if (x == revertedNum || x / 10 == revertedNum) return true; } return x == revertedNum || x == revertedNum / 10; }
相关文章推荐
- 【LeetCode】Python实现-234. 回文链表
- Leetcode 5最长回文子串 Java
- leetcode 5. Longest Palindromic Substring 最长回文子串的查找 + 按照length做DP
- leetcode之回文系列
- Leetcode 5. 最长回文子串
- LeetCode 9.回文数
- C++ LeetCode 5 最长回文子串
- Leetcode(9) - 回文数 - java版
- leetcode:最长回文子串
- leetcode-5-最长回文子串(longest palindromic substring)-java
- leetcode-5. Longest Palindromic Substring(寻找最长回文子串)
- 【Leetcode】234. Palindrome Linked List -判断链表是否为回文序列
- leetcode--回文数--python
- leetcode刷题总结之“整形回文”
- leetcode-9 回文数 java实现
- [LeetCode] 680. Valid Palindrome II 验证回文字符串 II
- 验证回文字符串(leetcode)
- 【leetcode每日一题】【2019-05-06】9. 回文数
- LeetCode------------palindrome-partitioningII(回文2)
- LeetCode——5最长回文子串