-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_8.cpp
More file actions
38 lines (33 loc) · 1.01 KB
/
Copy pathleetcode_8.cpp
File metadata and controls
38 lines (33 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution {
public:
int myAtoi(string s) {
int index = 0;
int sign = 1;
long long result = 0; // 더 큰 자료형 사용
// 공백 무시
while(index < s.length() && s[index] == ' ') {
index++;
}
// 부호 처리
if(index < s.length() && s[index] == '-') {
sign = -1;
index++;
} else if(index < s.length() && s[index] == '+') {
sign = 1;
index++;
}
// 숫자 변환
while(index < s.length() && s[index] >= '0' && s[index] <= '9'){
result = result * 10 + (s[index] - '0');
// 현재 시점에서 오버플로우 체크
if(sign == 1 && result >= INT_MAX) {
return INT_MAX;
}
if(sign == -1 && -result <= INT_MIN) {
return INT_MIN;
}
index++;
}
return static_cast<int>(result * sign);
}
};