Skip to content

Commit 85243ba

Browse files
Refine
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 56790b9 commit 85243ba

File tree

5 files changed

+47
-20
lines changed

5 files changed

+47
-20
lines changed

002_add_two_numbers/add_two_numbers.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ static struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
1717
dummy.next = p;
1818
while (l1 != NULL || l2 != NULL) {
1919
int sum = 0;
20-
int last_carry = carry;
2120

2221
if (l1 != NULL) {
2322
sum += l1->val;
@@ -34,7 +33,7 @@ static struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
3433
l2 = l2->next;
3534
}
3635

37-
sum += last_carry;
36+
sum += carry;
3837
carry = sum / 10;
3938
p->val = sum % 10;
4039
prev = p;

003_longest_substring_without_repeat/longest_substring_without_repeat.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ static int lengthOfLongestSubstring(char *s)
1515
len++;
1616
} else {
1717
if (index - offset[*s] > len) {
18-
/* not include in substring, go on increasing */
18+
/* not included in sliding window, go on increasing */
1919
len++;
2020
} else {
21-
/* count from scratch */
21+
/* repetition in sliding window, count from scratch */
2222
len = index - offset[*s];
2323
}
2424
}

007_reverse_integer/reverse_integer.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@
22
#include <stdlib.h>
33
#include <limits.h>
44

5+
56
static int reverse(int x)
67
{
7-
int y = 0;
8+
int n = 0;
89
while (x != 0) {
9-
int n = x % 10;
10-
// Checking the over/underflow.
11-
// Actually, it should be y>(INT_MAX-n)/10, but n/10 is 0, so omit it.
12-
if (y > INT_MAX / 10 || y < INT_MIN / 10) {
10+
int r = x % 10;
11+
/* Checking the over/underflow. */
12+
if (n > INT_MAX / 10 || (n == INT_MAX / 10 && r > 7)) {
13+
return 0;
14+
}
15+
if (n < INT_MIN / 10 || (n == INT_MIN / 10 && r < -8)) {
1316
return 0;
1417
}
15-
y = y * 10 + n;
18+
19+
n = n * 10 + r;
1620
x /= 10;
1721
}
18-
return y;
22+
return n;
1923
}
2024

2125
#define TEST(n, e) printf("%12d => %-12d %s!\n", n, reverse(n), e == reverse(n)?"passed":"failed")

008_atoi/atoi.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
1+
#include <ctype.h>
12
#include <stdio.h>
23
#include <stdlib.h>
34
#include <limits.h>
45

6+
57
static int myAtoi(char* str)
68
{
79
char *s;
8-
int n = 0, sign = 0;
10+
long n = 0;
11+
int sign = 0;
912

1013
while (*str == ' ' || *str == '\t') {
1114
str++;
1215
}
1316

17+
if (*str == '-') {
18+
if (isdigit(*++str)) {
19+
sign = 1;
20+
} else {
21+
return 0;
22+
}
23+
}
24+
25+
if (*str == '+') {
26+
if (isdigit(*++str)) {
27+
sign = 0;
28+
} else {
29+
return 0;
30+
}
31+
}
32+
1433
for (s = str; *s != '\0'; s++) {
1534
if (isdigit(*s)) {
1635
int d = *s - '0';
@@ -26,10 +45,6 @@ static int myAtoi(char* str)
2645
}
2746
}
2847
n = n * 10 + d;
29-
} else if (*s == '-' && isdigit(*(s + 1)) && (n == 0)) {
30-
sign = 1;
31-
} else if (*s == '+' && isdigit(*(s + 1)) && (n == 0)) {
32-
sign = 0;
3348
} else {
3449
break;
3550
}

009_palindrome_number/palindrome_number.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22
#include <stdlib.h>
33
#include <stdbool.h>
44

5+
56
static int reverse(int x)
67
{
7-
int y = 0;
8-
while (x) {
9-
y = y * 10 + x % 10;
8+
int n = 0;
9+
while (x != 0) {
10+
int r = x % 10;
11+
/* Checking the over/underflow. */
12+
if (n > INT_MAX / 10 || (n == INT_MAX / 10 && r > 7)) {
13+
return 0;
14+
}
15+
if (n < INT_MIN / 10 || (n == INT_MIN / 10 && r < -8)) {
16+
return 0;
17+
}
18+
n = n * 10 + r;
1019
x /= 10;
1120
}
12-
return y;
21+
return n;
1322
}
1423

1524
static bool isPalindrome(int x)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy