【LeetCode從零單排】No.7 Reverse Integer
前話
今天開始勵志刷一下leetcode上面的題目(還好這個網站沒被TG和諧)。從easy的開始,數一下差不多有40道,爭取兩個月搞定。
題目
沒想到做的第一道題目,雖然看似簡單,卻費了很大週摺。
題目如下:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
剛看到這道題,首先蹦出的想法是把整數轉換為字串,然後前後位置換下再轉回int型,實事證明這樣是不可取的,因為當輸入的int型數字很大的時候,比如412851289525983,就會產生溢位,這樣在使用Integer.parseInt這種函式的時候會報錯,正確的做法是對數字進行int操作,利用取10的餘數獲得每位數字。下面展示下,錯誤答案和正確答案。
程式碼
1.正確解
public class Solution {
public int reverse(int x) {
int sum = 0;
while (Math.abs(x) != 0)
{
if(Math.abs(sum) > Integer.MAX_VALUE / 10)
{
return 0;
}
sum = sum * 10 + x % 10;
x = x / 10;
}
return sum;
}
}
2.錯誤解
public class Solution {
public int reverse(int x) {
if(Math.abs(x)>100){
return 0;
}
else{
String x_str=Integer.toString(x);
char[] x_char = x_str.toCharArray();
String x_reverse_str="";
//System.out.print(Character.isDigit(x_char[0]));
if(Character.isDigit(x_char[0])){
for(int i=(x_str.length()-1);i>=0;i--){
x_reverse_str+=x_char[i];
}
}
else{
x_reverse_str+=x_char[0];
for(int i=(x_str.length()-1);i>=1;i--){
x_reverse_str+=x_char[i];
}
}
return Integer.parseInt(x_reverse_str);}
}
}
/********************************
* 本文來自部落格 “李博Garvin“
* 轉載請標明出處:http://blog.csdn.net/buptgshengod
******************************************/
相關文章
- leetcode Reverse IntegerLeetCode
- Leetcode 7 Reverse IntegerLeetCode
- Reverse Integer leetcode javaLeetCodeJava
- Leetcode-Problem:Reverse IntegerLeetCode
- 【LeetCode從零單排】No.8 String to Integer (喪心病狂的一道題)LeetCode
- LeetCode 第 7 題(Reverse Integer)LeetCode
- 【LeetCode從零單排】No38.CountAndSayLeetCode
- [LeetCode] Reverse Integer 翻轉整數LeetCode
- 【LeetCode從零單排】No20.ValidParenthesesLeetCode
- 【LeetCode從零單排】No19.RemoveNthNodeFromEndofListLeetCodeREM
- 【LeetCode從零單排】No21.MergeTwoSortedListsLeetCode
- 【LeetCode從零單排】No27.Remove ElementLeetCodeREM
- 【LeetCode從零單排】No28 Implement strStr()LeetCode
- 【LeetCode從零單排】No22.Generate ParenthesesLeetCode
- 【LeetCode從零單排】No58.Length of Last WordLeetCodeAST
- 【LeetCode從零單排】No67.AddBinaryLeetCode
- 【LeetCode從零單排】No70.ClimbingStairsLeetCodeAI
- 【LeetCode從零單排】No.9 Palindrome NumberLeetCode
- 【LeetCode從零單排】No14.LongestCommonPrefixLeetCode
- 【LeetCode從零單排】No36 Valid SudokuLeetCode
- 【LeetCode從零單排】No221.Maximal SquareLeetCode
- LeetCode Reverse Integer(007)解法總結LeetCode
- LeetCode - 解題筆記 - 7 - Reverse IntegerLeetCode筆記
- 【LeetCode從零單排】No15 3SumLeetCode
- 【LeetCode從零單排】No189 .Rotate ArrayLeetCode
- 【LeetCode從零單排】No88.Merge Sorted ArrayLeetCode
- Mysql從零單排-1MySql
- 【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從零單排】No 3 Longest Substring Without Repeating CharactersLeetCode
- 【LeetCode從零單排】No129 Sum Root to Leaf NumbersLeetCode
- 【LeetCode從零單排】No.160 Intersection of Two Linked ListsLeetCode
- 從零單排學Redis【黃金】Redis
- 【LeetCode從零單排】No121 Best Time to Buy and Sell StockLeetCode