@@ -160,19 +160,16 @@ class Solution:
160
160
i = 0
161
161
while s[i] == ' ' :
162
162
i += 1
163
- # 仅包含空格
164
163
if i == n:
165
164
return 0
166
165
sign = - 1 if s[i] == ' -' else 1
167
166
if s[i] in [' -' , ' +' ]:
168
167
i += 1
169
168
res, flag = 0 , (2 ** 31 - 1 ) // 10
170
169
while i < n:
171
- # 非数字,跳出循环体
172
170
if not s[i].isdigit():
173
171
break
174
172
c = int (s[i])
175
- # 溢出判断
176
173
if res > flag or (res == flag and c > 7 ):
177
174
return 2 ** 31 - 1 if sign > 0 else - (2 ** 31 )
178
175
res = res * 10 + c
@@ -190,17 +187,14 @@ class Solution {
190
187
if (n == 0 ) return 0 ;
191
188
int i = 0 ;
192
189
while (s. charAt(i) == ' ' ) {
193
- // 仅包含空格
194
190
if (++ i == n) return 0 ;
195
191
}
196
192
int sign = 1 ;
197
193
if (s. charAt(i) == ' -' ) sign = - 1 ;
198
194
if (s. charAt(i) == ' -' || s. charAt(i) == ' +' ) ++ i;
199
195
int res = 0 , flag = Integer . MAX_VALUE / 10 ;
200
196
for (; i < n; ++ i) {
201
- // 非数字,跳出循环体
202
197
if (s. charAt(i) < ' 0' || s. charAt(i) > ' 9' ) break ;
203
- // 溢出判断
204
198
if (res > flag || (res == flag && s. charAt(i) > ' 7' ))
205
199
return sign > 0 ? Integer . MAX_VALUE : Integer . MIN_VALUE ;
206
200
res = res * 10 + (s. charAt(i) - ' 0' );
@@ -210,6 +204,36 @@ class Solution {
210
204
}
211
205
```
212
206
207
+ #### C++
208
+
209
+ ``` cpp
210
+ class Solution {
211
+ public:
212
+ int myAtoi(string s) {
213
+ int i = 0, n = s.size();
214
+ while (i < n && s[ i] == ' ')
215
+ ++i;
216
+
217
+ int sign = 1;
218
+ if (i < n && (s[i] == '-' || s[i] == '+')) {
219
+ sign = s[i] == '-' ? -1 : 1;
220
+ ++i;
221
+ }
222
+
223
+ int res = 0 ;
224
+ while (i < n && isdigit(s[i])) {
225
+ int digit = s[i] - '0';
226
+ if (res > INT_MAX / 10 || (res == INT_MAX / 10 && digit > INT_MAX % 10)) {
227
+ return sign == 1 ? INT_MAX : INT_MIN;
228
+ }
229
+ res = res * 10 + digit;
230
+ ++i;
231
+ }
232
+ return res * sign;
233
+ }
234
+ };
235
+ ```
236
+
213
237
#### Go
214
238
215
239
``` go
@@ -282,8 +306,6 @@ const myAtoi = function (str) {
282
306
#### C#
283
307
284
308
``` cs
285
- // https://leetcode.com/problems/string-to-integer-atoi/
286
-
287
309
public partial class Solution
288
310
{
289
311
public int MyAtoi (string str )
@@ -352,36 +374,6 @@ class Solution {
352
374
}
353
375
```
354
376
355
- #### C++
356
-
357
- ``` c++
358
- class Solution {
359
- public:
360
- int myAtoi(string s) {
361
- int i = 0, n = s.size();
362
- while (i < n && s[ i] == ' ')
363
- ++i;
364
-
365
- int sign = 1;
366
- if (i < n && (s[i] == '-' || s[i] == '+')) {
367
- sign = s[i] == '-' ? -1 : 1;
368
- ++i;
369
- }
370
-
371
- int res = 0 ;
372
- while (i < n && isdigit(s[i])) {
373
- int digit = s[i] - '0';
374
- if (res > INT_MAX / 10 || (res == INT_MAX / 10 && digit > INT_MAX % 10)) {
375
- return sign == 1 ? INT_MAX : INT_MIN;
376
- }
377
- res = res * 10 + digit;
378
- ++i;
379
- }
380
- return res * sign;
381
- }
382
- };
383
- ```
384
-
385
377
<!-- tabs:end -->
386
378
387
379
<!-- solution:end -->
0 commit comments