Skip to content

feat: Add cpp solution to lc problem No.0008 #4442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions solution/0000-0099/0008.String to Integer (atoi)/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,36 @@ class Solution {
}
```

#### C++

```cpp
class Solution {
public:
int myAtoi(string s) {
int i = 0, n = s.size();
while (i < n && s[i] == ' ')
++i;

int sign = 1;
if (i < n && (s[i] == '-' || s[i] == '+')) {
sign = s[i] == '-' ? -1 : 1;
++i;
}

int res = 0;
while (i < n && isdigit(s[i])) {
int digit = s[i] - '0';
if (res > INT_MAX / 10 || (res == INT_MAX / 10 && digit > INT_MAX % 10)) {
return sign == 1 ? INT_MAX : INT_MIN;
}
res = res * 10 + digit;
++i;
}
return res * sign;
}
};
```

#### Go

```go
Expand Down
38 changes: 30 additions & 8 deletions solution/0000-0099/0008.String to Integer (atoi)/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,16 @@ class Solution:
i = 0
while s[i] == ' ':
i += 1
# 仅包含空格
if i == n:
return 0
sign = -1 if s[i] == '-' else 1
if s[i] in ['-', '+']:
i += 1
res, flag = 0, (2**31 - 1) // 10
while i < n:
# 非数字,跳出循环体
if not s[i].isdigit():
break
c = int(s[i])
# 溢出判断
if res > flag or (res == flag and c > 7):
return 2**31 - 1 if sign > 0 else -(2**31)
res = res * 10 + c
Expand All @@ -190,17 +187,14 @@ class Solution {
if (n == 0) return 0;
int i = 0;
while (s.charAt(i) == ' ') {
// 仅包含空格
if (++i == n) return 0;
}
int sign = 1;
if (s.charAt(i) == '-') sign = -1;
if (s.charAt(i) == '-' || s.charAt(i) == '+') ++i;
int res = 0, flag = Integer.MAX_VALUE / 10;
for (; i < n; ++i) {
// 非数字,跳出循环体
if (s.charAt(i) < '0' || s.charAt(i) > '9') break;
// 溢出判断
if (res > flag || (res == flag && s.charAt(i) > '7'))
return sign > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
res = res * 10 + (s.charAt(i) - '0');
Expand All @@ -210,6 +204,36 @@ class Solution {
}
```

#### C++

```cpp
class Solution {
public:
int myAtoi(string s) {
int i = 0, n = s.size();
while (i < n && s[i] == ' ')
++i;

int sign = 1;
if (i < n && (s[i] == '-' || s[i] == '+')) {
sign = s[i] == '-' ? -1 : 1;
++i;
}

int res = 0;
while (i < n && isdigit(s[i])) {
int digit = s[i] - '0';
if (res > INT_MAX / 10 || (res == INT_MAX / 10 && digit > INT_MAX % 10)) {
return sign == 1 ? INT_MAX : INT_MIN;
}
res = res * 10 + digit;
++i;
}
return res * sign;
}
};
```

#### Go

```go
Expand Down Expand Up @@ -282,8 +306,6 @@ const myAtoi = function (str) {
#### C#

```cs
// https://leetcode.com/problems/string-to-integer-atoi/

public partial class Solution
{
public int MyAtoi(string str)
Expand Down
25 changes: 25 additions & 0 deletions solution/0000-0099/0008.String to Integer (atoi)/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public:
int myAtoi(string s) {
int i = 0, n = s.size();
while (i < n && s[i] == ' ')
++i;

int sign = 1;
if (i < n && (s[i] == '-' || s[i] == '+')) {
sign = s[i] == '-' ? -1 : 1;
++i;
}

int res = 0;
while (i < n && isdigit(s[i])) {
int digit = s[i] - '0';
if (res > INT_MAX / 10 || (res == INT_MAX / 10 && digit > INT_MAX % 10)) {
return sign == 1 ? INT_MAX : INT_MIN;
}
res = res * 10 + digit;
++i;
}
return res * sign;
}
};