LeetCode 第 9 題(Palindrome Number)
LeetCode 第 9 題(Palindrome Number)
Determine whether an integer is a palindrome. Do this without extra space.
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.
這道題很簡單,可以利用第 7 題的部分程式碼。 第 7 題將一個整數的各個十進位制位翻轉了。如果翻轉之後數字沒有變化就說明是個 palindrome。
而且我們也不用考慮所謂的整數溢位問題,因為發生溢位的數肯定不是 palindrome。因此,就有了下面的程式碼。
bool isPalindrome(int x)
{
if(x < 0) return false;
int ret = 0, xx = x;
do
{
ret = 10 * ret + xx % 10;
xx = xx / 10;
}while(xx);
return ret == x;
}
相關文章
- Leetcode 9 Palindrome NumberLeetCode
- [LeetCode] 9. Palindrome NumberLeetCode
- LeetCode - 9. Palindrome NumberLeetCode
- leetcode Palindrome NumberLeetCode
- 【LeetCode從零單排】No.9 Palindrome NumberLeetCode
- Palindrome Number leetcode javaLeetCodeJava
- LeetCode - 解題筆記 - 8 - Palindrome NumberLeetCode筆記
- 9 check Palindrome Number by using pythonPython
- LeetCode 第 125 題 (Valid Palindrome)LeetCode
- LeetCode 第 65 題(Valid Number)LeetCode
- leetcode第九題Palindrome Number 驗證迴文數字LeetCode
- LeetCode Palindrome Number(009)解法總結LeetCode
- LeetCode 第 191 題 (Number of 1 Bits)LeetCode
- leetcode學習筆記09 palindrome-numberLeetCode筆記
- [LeetCode] Palindrome Number 驗證迴文數字LeetCode
- leetcode的第9題:迴文數LeetCode
- leetcode刷題--Valid PalindromeLeetCode
- leetcode刷題--Happy NumberLeetCodeAPP
- leetcode刷題--Number of 1 BitsLeetCode
- LeetCode-Palindrome PairsLeetCodeAI
- Leetcode Valid PalindromeLeetCode
- leetcode刷題--Excel Sheet Column NumberLeetCodeExcel
- LeetCode 題解(252) : Find the Duplicate NumberLeetCode
- LeetCode-Palindrome Permutation IILeetCode
- 【LeetCode】Palindrome Pairs(336)LeetCodeAI
- LeetCode-Valid PalindromeLeetCode
- Leetcode: Palindrome Partitioning IILeetCode
- Leetcode Palindrome Linked ListLeetCode
- Valid Palindrome leetcode javaLeetCodeJava
- Palindrome Partitioning leetcode javaLeetCodeJava
- Leetcode Number of islandsLeetCode
- LeetCode:Largest NumberLeetCode
- [LeetCode] 336. Palindrome PairsLeetCodeAI
- LeetCode-Palindrome Linked ListLeetCode
- Leetcode-Palindrome Partitioning IILeetCode
- LeetCode: Valid Palindrome [125]LeetCode
- Palindrome Partitioning II Leetcode javaLeetCodeJava
- Leetcode第1~10題LeetCode