-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ8_StringToInteger.java
More file actions
102 lines (76 loc) · 3.02 KB
/
Q8_StringToInteger.java
File metadata and controls
102 lines (76 loc) · 3.02 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.gyanblog.leetcode;
/**
* Implement atoi which converts a string to an integer.
*
* The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned.
Note:
Only the space character ' ' is considered as whitespace character.
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.
* Explanation: https://www.gyanblog.com/gyan/coding-interview/leetcode-string-integer-atoi/
*
*/
public class Q8_StringToInteger {
public int myAtoi(String str) {
int s = 0;
boolean neg = false;
boolean foundFirstValidInteger = false;
int l = str.length();
for (int i=0; i<l; i++) {
char c = str.charAt(i);
if (!foundFirstValidInteger) {
if (c == ' ') continue;
if (c == '+') {
foundFirstValidInteger = true;
continue;
}
if (c == '-') {
foundFirstValidInteger = true;
neg = true;
continue;
}
}
if (c >= '0' && c <= '9') {
foundFirstValidInteger = true;
int num = c - '0';
if (s > Integer.MAX_VALUE/10 || (s == Integer.MAX_VALUE/10 && num > 7)) {
if (neg) return Integer.MIN_VALUE;
return Integer.MAX_VALUE;
}
s = s*10 + num;
}
else if (!foundFirstValidInteger) {
return 0;
}
else {
if (neg) return -s;
return s;
}
}
if (neg) return -s;
return s;
}
public static void main(String[] args) {
Q8_StringToInteger q7 = new Q8_StringToInteger();
String str = " -42";
System.out.println("\"" + str + "\" -> " + q7.myAtoi(str));
str = "42";
System.out.println("\"" + str + "\" -> " + q7.myAtoi(str));
str = "4193 with words";
System.out.println("\"" + str + "\" -> " + q7.myAtoi(str));
str = "words and 987";
System.out.println("\"" + str + "\" -> " + q7.myAtoi(str));
str = "-91283472332";
System.out.println("\"" + str + "\" -> " + q7.myAtoi(str));
str = "3.14159";
System.out.println("\"" + str + "\" -> " + q7.myAtoi(str));
str = "+-2";
System.out.println("\"" + str + "\" -> " + q7.myAtoi(str));
str = " -0012a42";
System.out.println("\"" + str + "\" -> " + q7.myAtoi(str));
str = "2147483648";
System.out.println("\"" + str + "\" -> " + q7.myAtoi(str));
}
}