Skip to content

Commit 394c41d

Browse files
committed
2 parents 7c546ef + 67e39b5 commit 394c41d

File tree

230 files changed

+39475
-32343
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

230 files changed

+39475
-32343
lines changed

images/starcharts.svg

Lines changed: 30560 additions & 30460 deletions
Loading

solution/0000-0099/0002.Add Two Numbers/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ class Solution {
147147
class Solution {
148148
public:
149149
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
150-
ListNode* dummy = new ListNode();
150+
ListNode dummy;
151151
int carry = 0;
152-
ListNode* cur = dummy;
152+
ListNode* cur = &dummy;
153153
while (l1 || l2 || carry) {
154154
int s = (l1 ? l1->val : 0) + (l2 ? l2->val : 0) + carry;
155155
carry = s / 10;
@@ -158,7 +158,7 @@ public:
158158
l1 = l1 ? l1->next : nullptr;
159159
l2 = l2 ? l2->next : nullptr;
160160
}
161-
return dummy->next;
161+
return dummy.next;
162162
}
163163
};
164164
```

solution/0000-0099/0002.Add Two Numbers/README_EN.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ class Solution {
143143
class Solution {
144144
public:
145145
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
146-
ListNode* dummy = new ListNode();
146+
ListNode dummy;
147147
int carry = 0;
148-
ListNode* cur = dummy;
148+
ListNode* cur = &dummy;
149149
while (l1 || l2 || carry) {
150150
int s = (l1 ? l1->val : 0) + (l2 ? l2->val : 0) + carry;
151151
carry = s / 10;
@@ -154,7 +154,7 @@ public:
154154
l1 = l1 ? l1->next : nullptr;
155155
l2 = l2 ? l2->next : nullptr;
156156
}
157-
return dummy->next;
157+
return dummy.next;
158158
}
159159
};
160160
```

solution/0000-0099/0002.Add Two Numbers/Solution.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
class Solution {
1212
public:
1313
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
14-
ListNode* dummy = new ListNode();
14+
ListNode dummy;
1515
int carry = 0;
16-
ListNode* cur = dummy;
16+
ListNode* cur = &dummy;
1717
while (l1 || l2 || carry) {
1818
int s = (l1 ? l1->val : 0) + (l2 ? l2->val : 0) + carry;
1919
carry = s / 10;
@@ -22,6 +22,6 @@ class Solution {
2222
l1 = l1 ? l1->next : nullptr;
2323
l2 = l2 ? l2->next : nullptr;
2424
}
25-
return dummy->next;
25+
return dummy.next;
2626
}
2727
};

solution/0000-0099/0005.Longest Palindromic Substring/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,6 @@ char *longestPalindrome(char *s) {
576576

577577
```
578578
579-
580579
<!-- tabs:end -->
581580
582581
<!-- solution:end -->

solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ class Solution {
527527

528528
#### C
529529

530-
``` c
530+
```c
531531
char *longestPalindrome(char *s) {
532532
int n = strlen(s);
533533
if (n == 0) {

solution/0000-0099/0006.Zigzag Conversion/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ class Solution {
495495

496496
#### C
497497

498-
``` C
498+
```C
499499
char *convert(char *s, int numRows) {
500500
if (numRows == 1 || numRows >= strlen(s)) {
501501
char *result = malloc(strlen(s) + 1);

solution/0000-0099/0006.Zigzag Conversion/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ class Solution {
491491
}
492492
```
493493

494-
#### c
494+
####
495495

496496
```c
497497
char *convert(char *s, int numRows) {

solution/0000-0099/0008.String to Integer (atoi)/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,36 @@ class Solution {
214214
}
215215
```
216216

217+
#### C++
218+
219+
```cpp
220+
class Solution {
221+
public:
222+
int myAtoi(string s) {
223+
int i = 0, n = s.size();
224+
while (i < n && s[i] == ' ')
225+
++i;
226+
227+
int sign = 1;
228+
if (i < n && (s[i] == '-' || s[i] == '+')) {
229+
sign = s[i] == '-' ? -1 : 1;
230+
++i;
231+
}
232+
233+
int res = 0;
234+
while (i < n && isdigit(s[i])) {
235+
int digit = s[i] - '0';
236+
if (res > INT_MAX / 10 || (res == INT_MAX / 10 && digit > INT_MAX % 10)) {
237+
return sign == 1 ? INT_MAX : INT_MIN;
238+
}
239+
res = res * 10 + digit;
240+
++i;
241+
}
242+
return res * sign;
243+
}
244+
};
245+
```
246+
217247
#### Go
218248

219249
```go

solution/0000-0099/0008.String to Integer (atoi)/README_EN.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,19 +160,16 @@ class Solution:
160160
i = 0
161161
while s[i] == ' ':
162162
i += 1
163-
# 仅包含空格
164163
if i == n:
165164
return 0
166165
sign = -1 if s[i] == '-' else 1
167166
if s[i] in ['-', '+']:
168167
i += 1
169168
res, flag = 0, (2**31 - 1) // 10
170169
while i < n:
171-
# 非数字,跳出循环体
172170
if not s[i].isdigit():
173171
break
174172
c = int(s[i])
175-
# 溢出判断
176173
if res > flag or (res == flag and c > 7):
177174
return 2**31 - 1 if sign > 0 else -(2**31)
178175
res = res * 10 + c
@@ -190,17 +187,14 @@ class Solution {
190187
if (n == 0) return 0;
191188
int i = 0;
192189
while (s.charAt(i) == ' ') {
193-
// 仅包含空格
194190
if (++i == n) return 0;
195191
}
196192
int sign = 1;
197193
if (s.charAt(i) == '-') sign = -1;
198194
if (s.charAt(i) == '-' || s.charAt(i) == '+') ++i;
199195
int res = 0, flag = Integer.MAX_VALUE / 10;
200196
for (; i < n; ++i) {
201-
// 非数字,跳出循环体
202197
if (s.charAt(i) < '0' || s.charAt(i) > '9') break;
203-
// 溢出判断
204198
if (res > flag || (res == flag && s.charAt(i) > '7'))
205199
return sign > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
206200
res = res * 10 + (s.charAt(i) - '0');
@@ -210,6 +204,36 @@ class Solution {
210204
}
211205
```
212206

207+
#### C++
208+
209+
```cpp
210+
class Solution {
211+
public:
212+
int myAtoi(string s) {
213+
int i = 0, n = s.size();
214+
while (i < n && s[i] == ' ')
215+
++i;
216+
217+
int sign = 1;
218+
if (i < n && (s[i] == '-' || s[i] == '+')) {
219+
sign = s[i] == '-' ? -1 : 1;
220+
++i;
221+
}
222+
223+
int res = 0;
224+
while (i < n && isdigit(s[i])) {
225+
int digit = s[i] - '0';
226+
if (res > INT_MAX / 10 || (res == INT_MAX / 10 && digit > INT_MAX % 10)) {
227+
return sign == 1 ? INT_MAX : INT_MIN;
228+
}
229+
res = res * 10 + digit;
230+
++i;
231+
}
232+
return res * sign;
233+
}
234+
};
235+
```
236+
213237
#### Go
214238

215239
```go
@@ -282,8 +306,6 @@ const myAtoi = function (str) {
282306
#### C#
283307

284308
```cs
285-
// https://leetcode.com/problems/string-to-integer-atoi/
286-
287309
public partial class Solution
288310
{
289311
public int MyAtoi(string str)

0 commit comments

Comments
 (0)