【LeetCode從零單排】No.9 Palindrome Number
題目
這道題是迄今為止最快通過的一道題,改了兩次就過了,runtime一般(中等偏下,這點不太滿意)。Palindrome就是判斷一個整數是否對稱。
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.
程式碼
public class Solution {
public boolean isPalindrome(int x) {
if(x<0){
return false;
}
else{
if(x==0){
return true;
}
else{
int temp=0;
int temp_x=0;
temp=x;
while(temp/10!=0 || (temp<=9 && temp>0)){
temp_x=temp_x*10;
temp_x+=temp%10;
temp=temp/10;
}
// System.out.print(""+temp_x);
if(x==temp_x){
return true;
}
else{
return false;
}
}
}
}
}
/********************************
* 本文來自部落格 “李博Garvin“
* 轉載請標明出處:http://blog.csdn.net/buptgshengod
******************************************/
相關文章
- leetcode Palindrome NumberLeetCode
- 【LeetCode從零單排】No 191.Number of 1 Bits(考察位運算)LeetCode
- Leetcode 9 Palindrome NumberLeetCode
- Palindrome Number leetcode javaLeetCodeJava
- 【LeetCode從零單排】No38.CountAndSayLeetCode
- [LeetCode] 9. Palindrome NumberLeetCode
- LeetCode - 9. Palindrome NumberLeetCode
- 【LeetCode從零單排】No.7 Reverse IntegerLeetCode
- 【LeetCode從零單排】No20.ValidParenthesesLeetCode
- 【LeetCode從零單排】No19.RemoveNthNodeFromEndofListLeetCodeREM
- 【LeetCode從零單排】No21.MergeTwoSortedListsLeetCode
- 【LeetCode從零單排】No27.Remove ElementLeetCodeREM
- 【LeetCode從零單排】No28 Implement strStr()LeetCode
- 【LeetCode從零單排】No22.Generate ParenthesesLeetCode
- LeetCode 第 9 題(Palindrome Number)LeetCode
- 【LeetCode從零單排】No58.Length of Last WordLeetCodeAST
- 【LeetCode從零單排】No67.AddBinaryLeetCode
- 【LeetCode從零單排】No70.ClimbingStairsLeetCodeAI
- 【LeetCode從零單排】No14.LongestCommonPrefixLeetCode
- 【LeetCode從零單排】No36 Valid SudokuLeetCode
- 【LeetCode從零單排】No221.Maximal SquareLeetCode
- 【LeetCode從零單排】No15 3SumLeetCode
- 【LeetCode從零單排】No189 .Rotate ArrayLeetCode
- 【LeetCode從零單排】No88.Merge Sorted ArrayLeetCode
- Mysql從零單排-1MySql
- LeetCode Palindrome Number(009)解法總結LeetCode
- LeetCode - 解題筆記 - 8 - Palindrome NumberLeetCode筆記
- 【LeetCode從零單排】No96 Unique Binary Search TreesLeetCode
- 【LeetCode從零單排】No112 Path SumLeetCode
- 【LeetCode從零單排】No.169 Majority Element(hashmap用法)LeetCodeHashMap
- 【LeetCode從零單排】No83 Remove Duplicates from Sorted ListLeetCodeREM
- 【LeetCode從零單排】No26.Remove Duplicates from Sorted ArrayLeetCodeREM
- 從零單排學Redis【白銀】Redis
- leetcode學習筆記09 palindrome-numberLeetCode筆記
- [LeetCode] Palindrome Number 驗證迴文數字LeetCode
- 【LeetCode從零單排】No 3 Longest Substring Without Repeating CharactersLeetCode
- 【LeetCode從零單排】No129 Sum Root to Leaf NumbersLeetCode
- 【LeetCode從零單排】No.160 Intersection of Two Linked ListsLeetCode