【LeetCode從零單排】No.8 String to Integer (喪心病狂的一道題)
題目
題目很簡單,就是寫一個函式把string轉換成int,但是通過率只有可憐的11%,難點是要考慮所有情況,特別是int溢位邊界,反正我是寫了2個小時還沒解決,先放到這,有空接著搞,現在應該還有最後一個bug。
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
程式碼
public class Solution {
public int atoi(String str) {
String num_str="";
char[] str_char=str.toCharArray();
char[] sympo="-+".toCharArray();
boolean abs=true;
for(int i=0;i<str.length();i++){
if(str_char[i]==sympo[0] ||str_char[i]==sympo[1]){
if(!Character.isDigit(str_char[i+1])){
return 0;
}
if(str_char[i]==sympo[0]){
abs=false;
}
}
if(Character.isDigit(str_char[i])){
num_str+=String.valueOf(str_char[i]);
}
else{
if(num_str.length()!=0){
break;
}
}
// if(Character.isDigit(str_char[i])){
// num_str+=String.valueOf(str_char[i]);
// }
if(num_str!=""){
if(Math.abs(Integer.parseInt(num_str))>=Integer.MAX_VALUE / 10){
return Integer.MAX_VALUE;
}
}
}
if(num_str!=""){
if(abs){
return Integer.parseInt(num_str);
}
else{
return -Integer.parseInt(num_str);
}
}
else{
return 0;
}
}
}
/********************************
* 本文來自部落格 “李博Garvin“
* 轉載請標明出處:http://blog.csdn.net/buptgshengod
******************************************/
相關文章
- 【LeetCode從零單排】No.7 Reverse IntegerLeetCode
- 【LeetCode從零單排】No38.CountAndSayLeetCode
- 從一道面試題探究 Integer 的實現面試題
- 【LeetCode從零單排】No20.ValidParenthesesLeetCode
- 【LeetCode從零單排】No19.RemoveNthNodeFromEndofListLeetCodeREM
- 【LeetCode從零單排】No21.MergeTwoSortedListsLeetCode
- 【LeetCode從零單排】No27.Remove ElementLeetCodeREM
- 【LeetCode從零單排】No28 Implement strStr()LeetCode
- 【LeetCode從零單排】No22.Generate ParenthesesLeetCode
- leetcode String to Integer (atoi)LeetCode
- Leetcode - String to Integer (atoi)LeetCode
- 【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從零單排】No15 3SumLeetCode
- 【LeetCode從零單排】No189 .Rotate ArrayLeetCode
- 【LeetCode從零單排】No88.Merge Sorted ArrayLeetCode
- Leetcode 8 String to Integer (atoi)LeetCode
- String to Integer (atoi) leetcode javaLeetCodeJava
- 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
- 喪心病狂的Android混淆檔案生成器Android
- 從零單排學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
- 【LeetCode從零單排】No118 Pascal's TriangleLeetCode
- 【LeetCode從零單排】No104 Maximum Depth of Binary TreeLeetCode