Skip to content

Commit dc734b0

Browse files
committed
leetcode
1 parent 080e692 commit dc734b0

File tree

3 files changed

+216
-0
lines changed

3 files changed

+216
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
func myAtoi(s string) int {
4+
if len(s) == 0 {
5+
return 0
6+
}
7+
8+
sign := 1 // -1 negative
9+
index := 0
10+
val := 0
11+
12+
for len(s) > index && s[index] == ' ' {
13+
index++
14+
}
15+
16+
if index >= len(s) {
17+
return 0
18+
}
19+
20+
if v := s[index]; v == '-' || v == '+' {
21+
if v == '-' {
22+
sign = -1
23+
}
24+
25+
index++
26+
}
27+
28+
for {
29+
if index == len(s) {
30+
break
31+
}
32+
33+
if isNumericRune(s[index]) == false {
34+
return sign * val
35+
}
36+
37+
val = val*10 + int(s[index]) - '0'
38+
39+
if val > 2147483647 || val < -2147483648 {
40+
if sign == 1 {
41+
return 2147483647
42+
}
43+
44+
return -2147483648
45+
}
46+
47+
index++
48+
}
49+
50+
return sign * val
51+
}
52+
53+
func isNumericRune(x byte) bool {
54+
return x >= '0' && x <= '9'
55+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
2+
# 🔥 String To Integer (Atoi) 🔥|| Simple Fast and Easy || with Explanation
3+
4+
Converts a string to an integer, mimicking the behavior of the C `atoi` function.
5+
6+
**Explanation:**
7+
8+
1. **Whitespace Removal:**
9+
* The input string `s` is stripped of leading and trailing whitespace using `s.strip()`.
10+
2. **Empty String Check:**
11+
* If the stripped string is empty, the function returns 0.
12+
3. **Sign Determination:**
13+
* The code checks the first character of the string for a sign (`'+'` or `'-'`).
14+
* If a `'-'` is found, the `sign` variable is set to -1; otherwise, it defaults to 1.
15+
* The `index` is incremented to move past the sign character.
16+
4. **Digit Extraction and Conversion:**
17+
* The code iterates through the remaining characters of the string as long as they are digits.
18+
* For each digit, it converts the character to its integer equivalent using `int(s[index])` and adds it to the `val` variable.
19+
* The `val` variable is multiplied by 10 in each iteration to construct the integer.
20+
5. **Overflow Handling:**
21+
* During the integer construction, the code checks if `val` exceeds the 32-bit signed integer range (`[-2147483648, 2147483647]`).
22+
* If an overflow occurs:
23+
* If the `sign` is positive, it returns `2147483647`.
24+
* If the `sign` is negative, it returns `-2147483648`.
25+
6. **Return Value:**
26+
* Finally, the function returns the calculated integer value (`sign * val`).
27+
28+
## **Code:**
29+
30+
```python
31+
class Solution:
32+
def myAtoi(self, s: str) -> int:
33+
s = s.strip()
34+
if not s:
35+
return 0
36+
37+
sign = 1
38+
index = 0
39+
val = 0
40+
41+
if s[0] == '-':
42+
sign = -1
43+
index += 1
44+
elif s[0] == '+':
45+
index += 1
46+
47+
while index < len(s) and s[index].isdigit():
48+
val = val * 10 + int(s[index])
49+
if val > 2147483647 or val < -2147483648:
50+
if sign == 1:
51+
return 2147483647
52+
else:
53+
return -2147483648
54+
index += 1
55+
56+
return sign * val
57+
```
58+
59+
60+
## **Code - Golang**
61+
62+
```go
63+
func myAtoi(s string) int {
64+
if len(s) == 0 {
65+
return 0
66+
}
67+
68+
sign := 1 // -1 negative
69+
index := 0
70+
val := 0
71+
72+
for len(s) > index && s[index] == ' ' {
73+
index++
74+
}
75+
76+
if index >= len(s) {
77+
return 0
78+
}
79+
80+
if v := s[index]; v == '-' || v == '+' {
81+
if v == '-' {
82+
sign = -1
83+
}
84+
85+
index++
86+
}
87+
88+
for {
89+
if index == len(s) {
90+
break
91+
}
92+
93+
if isNumericRune(s[index]) == false {
94+
return sign * val
95+
}
96+
97+
val = val*10 + int(s[index]) - '0'
98+
99+
if val > 2147483647 || val < -2147483648 {
100+
if sign == 1 {
101+
return 2147483647
102+
}
103+
104+
return -2147483648
105+
}
106+
107+
index++
108+
}
109+
110+
return sign * val
111+
}
112+
113+
func isNumericRune(x byte) bool {
114+
return x >= '0' && x <= '9'
115+
}
116+
117+
```
118+
119+
120+
## Space and Time Complexity
121+
122+
**Time Complexity:**
123+
124+
* **O(n)**, where `n` is the length of the input string `s`.
125+
* The `strip()` operation takes O(n) time in the worst case.
126+
* The `while` loop iterates through the string at most once.
127+
128+
**Space Complexity:**
129+
130+
* **O(1)** (constant).
131+
* The code uses a fixed number of variables (`sign`, `index`, `val`), regardless of the input string's size.
132+
* The `strip()` operation in python generally returns a new string, but in the case of no leading or trailing white space, it will return the same string. Therefore the space complexity remains constant.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def myAtoi(self, s: str) -> int:
3+
4+
s = s.strip()
5+
if not s:
6+
return 0
7+
8+
sign = 1
9+
index = 0
10+
val = 0
11+
12+
if s[0] == "-":
13+
sign = -1
14+
index += 1
15+
elif s[0] == "+":
16+
index += 1
17+
18+
while index < len(s) and s[index].isdigit():
19+
val = val * 10 + int(s[index])
20+
21+
if val > 2147483647 or val < -2147483648:
22+
if sign == 1:
23+
return 2147483647
24+
else:
25+
return -2147483648
26+
27+
index += 1
28+
29+
return sign * val

0 commit comments

Comments
 (0)