LeetCode - Algorithms - 8. String to Integer (atoi)

Problem

8. String to Integer (atoi)

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Solution {
public int myAtoi(String s) {
int re = 0;
long num = 0;
s = s.trim();
char[] a = s.toCharArray();
int len = a.length;
int[] digits = new int[len];
int k = 0;
boolean isNegative = false;
char c = ' ';
if (len > 0) {
boolean hasSignFlag = false;
if (a[0] == '+' || a[0] == '-')
hasSignFlag = true;
if (a[0] == '-')
isNegative = true;
if (a[0] == '+')
isNegative = false;
for (int i = hasSignFlag ? 1 : 0; i < len; i++) {
c = a[i];
if (Character.isDigit(c))
digits[k++] = Integer.parseInt(c + "");
else if (!Character.isDigit(c))
break;
else
;
}
}
for (int m = 0; m < k; m++)
num += digits[m] * Math.pow(10, k - m - 1);
if (isNegative)
num *= -1;
if (num < Integer.MIN_VALUE)
num = Integer.MIN_VALUE;
if (num > Integer.MAX_VALUE)
num = Integer.MAX_VALUE;
re = new Long(num).intValue();
return re;
}
}

Submission Detail

  • 1082 / 1082 test cases passed.
  • Runtime: 5 ms, faster than 26.19% of Java online submissions for String to Integer (atoi).
  • Memory Usage: 38.9 MB, less than 80.12% of Java online submissions for String to Integer (atoi).