-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.string-to-integer-atoi.cpp
69 lines (60 loc) · 1.56 KB
/
8.string-to-integer-atoi.cpp
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// @before-stub-for-debug-begin
#include <vector>
#include <string>
#include "commoncppproblem8.h"
using namespace std;
// @before-stub-for-debug-end
/*
* @lc app=leetcode id=8 lang=cpp
*
* [8] String to Integer (atoi)
*/
#include <ctype.h>
#include <limits>
// @lc code=start
class Solution {
public:
int myAtoi(string s) {
// step 1: leading whitespace
int start = 0;
while (start < s.length() && s[start] == ' ') ++start;
if (start == s.length()) return 0;
// step 2: sign
bool negavive = false;
if (s[start] == '-')
{
negavive = true;
++start;
}
else if (s[start] == '+')
{
++start;
}
// step 3A: leading zeros
while (start < s.length() && s[start] == '0') ++start;
// step 3B: conversion
if (negavive) return toNegative(s, start);
int result = 0, max = numeric_limits<int>::max();
char c;
while (start < s.length() && isdigit(c = s[start++]))
{
int d = c - '0';
if (result > (max - d)/10) return max;
result = result * 10 + d;
}
return result;
}
int toNegative(string s, int start)
{
int result = 0, min = numeric_limits<int>::min();
char c;
while (start < s.length() && isdigit(c = s[start++]))
{
int d = c - '0';
if (result < (min + d)/10) return min;
result = result * 10 - d;
}
return result;
}
};
// @lc code=end