-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path008StringToInteger.cpp
More file actions
38 lines (32 loc) · 923 Bytes
/
008StringToInteger.cpp
File metadata and controls
38 lines (32 loc) · 923 Bytes
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
//
// Created by Pasco on 16/4/7.
//
#include "catch.hpp"
using namespace std;
class Solution {
public:
int myAtoi(string str) {
long result = 0;
int indicator = 1;
for(int i = 0; i<str.size();)
{
i = str.find_first_not_of(' ');
if(str[i] == '-' || str[i] == '+')
indicator = (str[i++] == '-')? -1 : 1;
while('0'<= str[i] && str[i] <= '9')
{
result = result*10 + (str[i++]-'0');
if(result*indicator >= INT_MAX) return INT_MAX;
if(result*indicator <= INT_MIN) return INT_MIN;
}
return result*indicator;
}
}
};
TEST_CASE("CASE 1 for string to integer") {
Solution solution;
string foo = "-2147483648";
int expectOutput = -2147483648;
int actualOutput = solution.myAtoi(foo);
REQUIRE(expectOutput == actualOutput);
}