Skip to content

Commit bca9136

Browse files
committed
Solution done for 0008 and 0009 in c language
1 parent 0cca36a commit bca9136

File tree

6 files changed

+152
-0
lines changed

6 files changed

+152
-0
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,39 @@ class Solution {
385385
}
386386
}
387387
```
388+
#### C
389+
``` C
390+
int myAtoi(char* s) {
391+
int i = 0;
392+
int sign = 1;
393+
long result = 0;
394+
while (s[i] == ' ') {
395+
i++;
396+
}
397+
if (s[i] == '-' || s[i] == '+') {
398+
sign = (s[i] == '-') ? -1 : 1;
399+
i++;
400+
}
401+
while (isdigit(s[i])) {
402+
result = result * 10 + (s[i] - '0');
403+
if (sign == 1 && result > INT_MAX) {
404+
return INT_MAX;
405+
}
406+
if (sign == -1 && -result < INT_MIN) {
407+
return INT_MIN;
408+
}
409+
i++;
410+
}
411+
b
412+
result
413+
*= sign;
414+
if (result > INT_MAX)
415+
return INT_MAX;
416+
if (result < INT_MIN)
417+
return INT_MIN;
418+
return (int) result;
419+
}
420+
```
388421
389422
<!-- tabs:end -->
390423

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

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

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

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

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

251+
#### C
252+
253+
``` C
254+
bool isPalindrome(int x) {
255+
if (x < 0)
256+
return false;
257+
int original = x;
258+
int reversed = 0;
259+
while (x != 0) {
260+
int digit = x % 10;
261+
if (reversed > (2147483647 - digit) / 10)
262+
return false;
263+
reversed = reversed * 10 + digit;
264+
x /= 10;
265+
}
266+
return original == reversed;
267+
}
268+
269+
```
270+
251271
<!-- tabs:end -->
252272
253273
<!-- solution:end -->

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

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

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

0 commit comments

Comments
 (0)