|
| 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. |
0 commit comments