You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 0001-0500/0008. String to Integer (atoi).swift
+20-13
Original file line number
Diff line number
Diff line change
@@ -12,11 +12,11 @@ class Solution {
12
12
// Convert these digits into an integer (i.e. "123" -> 123, "0032" -> 32). If no digits were read, then the integer is 0. Change the sign as necessary (from step 2).
13
13
// If the integer is out of the 32-bit signed integer range [-2^31, 2^31 - 1], then clamp the integer so that it remains in the range. Specifically, integers less than -2^31 should be clamped to -2^31, and integers greater than 2^31 - 1 should be clamped to 2^31 - 1.
14
14
// Return the integer as the final result.
15
-
15
+
16
16
// Note:
17
17
// Only the space character ' ' is considered a whitespace character.
18
18
// Do not ignore any characters other than the leading whitespace or the rest of the string after the digits.
19
-
19
+
20
20
// Example 1:
21
21
// Input: s = "42"
22
22
// Output: 42
@@ -29,7 +29,7 @@ class Solution {
29
29
// ^
30
30
// The parsed integer is 42.
31
31
// Since 42 is in the range [-2^31, 2^31 - 1], the final result is 42.
32
-
32
+
33
33
// Example 2:
34
34
// Input: s = " -42"
35
35
// Output: -42
@@ -42,7 +42,7 @@ class Solution {
42
42
// ^
43
43
// The parsed integer is -42.
44
44
// Since -42 is in the range [-2^31, 2^31 - 1], the final result is -42.
45
-
45
+
46
46
// Example 3:
47
47
// Input: s = "4193 with words"
48
48
// Output: 4193
@@ -55,7 +55,7 @@ class Solution {
55
55
// ^
56
56
// The parsed integer is 4193.
57
57
// Since 4193 is in the range [-2^31, 2^31 - 1], the final result is 4193.
58
-
58
+
59
59
// Example 4:
60
60
// Input: s = "words and 987"
61
61
// Output: 0
@@ -68,7 +68,7 @@ class Solution {
68
68
// ^
69
69
// The parsed integer is 0 because no digits were read.
70
70
// Since 0 is in the range [-2^31, 2^31 - 1], the final result is 0.
71
-
71
+
72
72
// Example 5:
73
73
// Input: s = "-91283472332"
74
74
// Output: -2147483648
@@ -81,7 +81,7 @@ class Solution {
81
81
// ^
82
82
// The parsed integer is -91283472332.
83
83
// Since -91283472332 is less than the lower bound of the range [-2^31, 2^31 - 1], the final result is clamped to -2^31 = -2147483648.
84
-
84
+
85
85
// Constraints:
86
86
// 0 <= s.length <= 200
87
87
// s consists of English letters (lower-case and upper-case), digits (0-9), ' ', '+', '-', and '.'.
@@ -101,8 +101,9 @@ class Solution {
101
101
if isLeading ==false{ isValid =false}
102
102
case"-","+":
103
103
isLeading =false
104
-
if isSign || nums.count >0{ isValid =false}
105
-
else{
104
+
if isSign || nums.count >0{
105
+
isValid =false
106
+
}else{
106
107
isSign =true
107
108
isNegtive = c =="-"?true:false
108
109
}
@@ -117,12 +118,18 @@ class Solution {
117
118
fornumin nums {
118
119
if num ==0 && isLeadingZero {continue}else{ isLeadingZero =false}
119
120
res = num +10* res
120
-
121
-
if isNegtive && res >Int(pow(2.0,31)){ res =Int(pow(2.0,31)); break}
122
-
if !isNegtive && res >Int(pow(2.0,31))-1{ res =Int(pow(2.0,31))-1; break}
0 commit comments