Skip to content

Commit 590343f

Browse files
feat: add c solutions to lc problems: No.0008,0009 (#4506)
1 parent ac79b4f commit 590343f

File tree

6 files changed

+134
-0
lines changed

6 files changed

+134
-0
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,36 @@ class Solution {
386386
}
387387
```
388388

389+
#### C
390+
391+
```c
392+
int myAtoi(char* s) {
393+
int i = 0;
394+
395+
while (s[i] == ' ') {
396+
i++;
397+
}
398+
399+
int sign = 1;
400+
if (s[i] == '-' || s[i] == '+') {
401+
sign = (s[i] == '-') ? -1 : 1;
402+
i++;
403+
}
404+
405+
int res = 0;
406+
while (isdigit(s[i])) {
407+
int digit = s[i] - '0';
408+
if (res > INT_MAX / 10 || (res == INT_MAX / 10 && digit > INT_MAX % 10)) {
409+
return sign == 1 ? INT_MAX : INT_MIN;
410+
}
411+
res = res * 10 + digit;
412+
i++;
413+
}
414+
415+
return res * sign;
416+
}
417+
```
418+
389419
<!-- tabs:end -->
390420
391421
<!-- solution:end -->

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,36 @@ class Solution {
374374
}
375375
```
376376

377+
#### C
378+
379+
```c
380+
int myAtoi(char* s) {
381+
int i = 0;
382+
383+
while (s[i] == ' ') {
384+
i++;
385+
}
386+
387+
int sign = 1;
388+
if (s[i] == '-' || s[i] == '+') {
389+
sign = (s[i] == '-') ? -1 : 1;
390+
i++;
391+
}
392+
393+
int res = 0;
394+
while (isdigit(s[i])) {
395+
int digit = s[i] - '0';
396+
if (res > INT_MAX / 10 || (res == INT_MAX / 10 && digit > INT_MAX % 10)) {
397+
return sign == 1 ? INT_MAX : INT_MIN;
398+
}
399+
res = res * 10 + digit;
400+
i++;
401+
}
402+
403+
return res * sign;
404+
}
405+
```
406+
377407
<!-- tabs:end -->
378408
379409
<!-- solution:end -->
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
int myAtoi(char* s) {
2+
int i = 0;
3+
4+
while (s[i] == ' ') {
5+
i++;
6+
}
7+
8+
int sign = 1;
9+
if (s[i] == '-' || s[i] == '+') {
10+
sign = (s[i] == '-') ? -1 : 1;
11+
i++;
12+
}
13+
14+
int res = 0;
15+
while (isdigit(s[i])) {
16+
int digit = s[i] - '0';
17+
if (res > INT_MAX / 10 || (res == INT_MAX / 10 && digit > INT_MAX % 10)) {
18+
return sign == 1 ? INT_MAX : INT_MIN;
19+
}
20+
res = res * 10 + digit;
21+
i++;
22+
}
23+
24+
return res * sign;
25+
}

solution/0000-0099/0009.Palindrome Number/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,24 @@ class Solution {
248248
}
249249
```
250250

251+
#### C
252+
253+
```c
254+
bool isPalindrome(int x) {
255+
if (x < 0 || (x != 0 && x % 10 == 0)) {
256+
return false;
257+
}
258+
259+
int y = 0;
260+
while (y < x) {
261+
y = y * 10 + x % 10;
262+
x /= 10;
263+
}
264+
265+
return (x == y || x == y / 10);
266+
}
267+
```
268+
251269
<!-- tabs:end -->
252270
253271
<!-- solution:end -->

solution/0000-0099/0009.Palindrome Number/README_EN.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,24 @@ class Solution {
240240
}
241241
```
242242

243+
#### C
244+
245+
```c
246+
bool isPalindrome(int x) {
247+
if (x < 0 || (x != 0 && x % 10 == 0)) {
248+
return false;
249+
}
250+
251+
int y = 0;
252+
while (y < x) {
253+
y = y * 10 + x % 10;
254+
x /= 10;
255+
}
256+
257+
return (x == y || x == y / 10);
258+
}
259+
```
260+
243261
<!-- tabs:end -->
244262
245263
<!-- solution:end -->
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
bool isPalindrome(int x) {
2+
if (x < 0 || (x != 0 && x % 10 == 0)) {
3+
return false;
4+
}
5+
6+
int y = 0;
7+
while (y < x) {
8+
y = y * 10 + x % 10;
9+
x /= 10;
10+
}
11+
12+
return (x == y || x == y / 10);
13+
}

0 commit comments

Comments
 (0)