Skip to content

Commit 2dec8a0

Browse files
authored
feat: add solution(s) to lc problem(s): No.67
1 parent e251f9e commit 2dec8a0

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

lcof/面试题67. 把字符串转换成整数/Solution.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,31 @@ class Solution {
33
int StrToInt(string str) {
44
int res = 0, bndry = INT_MAX / 10;
55
int i = 0, sign = 1, length = str.size();
6-
if(length == 0) {
6+
if (length == 0) {
77
return 0;
88
}
99
// 删除首部空格
10-
while(str[i] == ' '){
11-
if(++i == length) {
10+
while (str[i] == ' ') {
11+
if (++i == length) {
1212
return 0;
1313
}
1414
}
15-
//若有负号则标识符号位
16-
if(str[i] == '-') {
15+
// 若有负号则标识符号位
16+
if (str[i] == '-') {
1717
sign = -1;
1818
}
19-
if(str[i] == '-' || str[i] == '+') {
19+
if (str[i] == '-' || str[i] == '+') {
2020
i++;
2121
}
22-
for(int j = i; j < length; j++) {
23-
if(str[j] < '0' || str[j] > '9') {
22+
for (int j = i; j < length; j++) {
23+
if (str[j] < '0' || str[j] > '9') {
2424
break;
2525
}
26-
//res>214748364越界;res=214748364且str[j] > '7'越界
27-
if(res > bndry || res == bndry && str[j] > '7'){
26+
// res>214748364越界;res=214748364且str[j] > '7'越界
27+
if (res > bndry || res == bndry && str[j] > '7') {
2828
return sign == 1 ? INT_MAX : INT_MIN;
2929
}
30-
//从左向右遍历数字并更新结果
30+
// 从左向右遍历数字并更新结果
3131
res = res * 10 + (str[j] - '0');
3232
}
3333
return sign * res;

0 commit comments

Comments
 (0)