Leetcode - String to Integer (atoi)
Question:
My code:
public class Solution {
public int myAtoi(String str) {
if (str == null || str.length() == 0)
return 0;
boolean isZero = true;
boolean isPositive = true;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != ' ') {
str = str.substring(i, str.length());
break;
}
}
if (str.charAt(0) == '-') {
isPositive = false;
str = str.substring(1, str.length());
}
else if (str.charAt(0) == '+')
str = str.substring(1, str.length());
String result = "";
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
if (str.charAt(i) == '0' && isZero)
continue;
else {
isZero = false;
result += str.charAt(i);
}
}
else
break;;
}
if (result.length() == 0)
return 0;
if (result.length() > 10) {
if (isPositive)
return Integer.MAX_VALUE;
else
return Integer.MIN_VALUE;
}
long a = Long.parseLong(result);
if (!isPositive)
a = (-1) * a;
if (a < Integer.MIN_VALUE)
return Integer.MIN_VALUE;
else if (a > Integer.MAX_VALUE)
return Integer.MAX_VALUE;
else
return (int)a;
}
}
My test result:
這道題目沒有什麼意義,就是有一些極端情況,可能沒考慮到。測試的時候哪裡有錯,就把那些有錯的情況考慮到就好了。
**
總結:沒啥意義。
**
Anyway, Good luck, Richardo!
My code:
public class Solution {
public int myAtoi(String str) {
if (str == null || str.length() == 0) {
return 0;
}
int start = 0;
int base = 0;
int sign = 1;
int i = 0;
String s = str;
for (; i < str.length(); i++) {
if (s.charAt(i) != ' ') {
break;
}
}
if (s.charAt(i) == '+' || s.charAt(i) == '-') {
if (s.charAt(i) == '-') {
sign = -1;
}
i++;
}
while (i < s.length() && s.charAt(i) >= '0' && s.charAt(i) <= '9') {
char curr = s.charAt(i);
if (base > Integer.MAX_VALUE / 10 || (base == Integer.MAX_VALUE / 10 && curr - '0' > 7)) {
if (sign == 1) {
return Integer.MAX_VALUE;
}
else {
return Integer.MIN_VALUE;
}
}
base = 10 * base + (curr - '0');
i++;
}
return sign * base;
}
public static void main(String[] args) {
Solution test = new Solution();
int ret = test.myAtoi("1");
System.out.println(ret);
}
}
reference:
https://discuss.leetcode.com/topic/2666/my-simple-solution
這種題目沒有意思,看下答案就行了。然後記住它。
Anyway, Good luck, Richardo! -- 09/20/2016
相關文章
- Leetcode 8 String to Integer (atoi)LeetCode
- Leetcode 8. String to Integer (atoi) 字串轉整數 (atoi)LeetCode字串
- LeetCode String to Integer (atoi)(008)解法總結LeetCode
- String to Integer (atoi) 字串轉換整數 (atoi)字串
- LeetCode-8. 字串轉整數 (atoi)LeetCode字串
- Leetcode 12 Integer to RomanLeetCode
- Leetcode 13 Roman to IntegerLeetCode
- Leetcode 7 Reverse IntegerLeetCode
- Sting 轉List<String>轉List<Integer>
- Leetcode 273 Integer to English WordsLeetCode
- Leetcode 12. Integer to RomanLeetCode
- Integer.valueof(String s)和Integer.parseInt(String s)的具體區別是什麼?
- [LeetCode] Rotate StringLeetCode
- LeetCode Integer to Roman(012)解法總結LeetCode
- LeetCode Roman to Integer(013)解法總結LeetCode
- LeetCode Reverse Integer(007)解法總結LeetCode
- [leetcode] 1394. Find Lucky Integer in an ArrayLeetCode
- Leetcode 481 Magical StringLeetCode
- LeetCode 13. Roman to Integer C語言LeetCodeC語言
- LeetCode - 解題筆記 - 12 - Integer to RomanLeetCode筆記
- LeetCode - 解題筆記 - 7 - Reverse IntegerLeetCode筆記
- java-string轉換成integer的方式及原理Java
- List<Integer>裡有可能存String型別元素嗎?型別
- 【Leetcode】767. Reorganize StringLeetCode
- Leetcode 151 Reverse Words in a StringLeetCode
- leetcode 344. Reverse StringLeetCode
- 【Leetcode】1528. Shuffle StringLeetCode
- [LeetCode] 678. Valid Parenthesis StringLeetCode
- [LeetCode] 844. Backspace String CompareLeetCode
- [LeetCode] 3163. String Compression IIILeetCode
- LeetCode 438. Find All Anagrams in a StringLeetCode
- LeetCode 394. Decode String All In OneLeetCode
- 字串轉換整數(atoi)字串
- LeetCode之Construct String from Binary Tree(Kotlin)LeetCodeStructKotlin
- LeetCode C++ 387. First Unique Character in a String【String/Hash Table】簡單LeetCodeC++
- [LeetCode] 1545. Find Kth Bit in Nth Binary StringLeetCode
- [LeetCode] 2825. Make String a Subsequence Using Cyclic IncrementsLeetCodeREM
- [LeetCode] 1750. Minimum Length of String After Deleting Similar EndsLeetCodeMILA
- LeetCode 1209. Remove All Adjacent Duplicates in String II 有坑LeetCodeREM