diff --git a/solution/0000-0099/0008.String to Integer (atoi)/README.md b/solution/0000-0099/0008.String to Integer (atoi)/README.md index 87634a3e1f38a..324738cca1c5e 100644 --- a/solution/0000-0099/0008.String to Integer (atoi)/README.md +++ b/solution/0000-0099/0008.String to Integer (atoi)/README.md @@ -386,6 +386,36 @@ class Solution { } ``` +#### C + +```c +int myAtoi(char* s) { + int i = 0; + + while (s[i] == ' ') { + i++; + } + + int sign = 1; + if (s[i] == '-' || s[i] == '+') { + sign = (s[i] == '-') ? -1 : 1; + i++; + } + + int res = 0; + while (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; +} +``` + diff --git a/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md b/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md index 05ee6458e6711..bc0c422b89134 100644 --- a/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md +++ b/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md @@ -374,6 +374,36 @@ class Solution { } ``` +#### C + +```c +int myAtoi(char* s) { + int i = 0; + + while (s[i] == ' ') { + i++; + } + + int sign = 1; + if (s[i] == '-' || s[i] == '+') { + sign = (s[i] == '-') ? -1 : 1; + i++; + } + + int res = 0; + while (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; +} +``` + diff --git a/solution/0000-0099/0008.String to Integer (atoi)/Solution.c b/solution/0000-0099/0008.String to Integer (atoi)/Solution.c new file mode 100644 index 0000000000000..26f63ab4316e3 --- /dev/null +++ b/solution/0000-0099/0008.String to Integer (atoi)/Solution.c @@ -0,0 +1,25 @@ +int myAtoi(char* s) { + int i = 0; + + while (s[i] == ' ') { + i++; + } + + int sign = 1; + if (s[i] == '-' || s[i] == '+') { + sign = (s[i] == '-') ? -1 : 1; + i++; + } + + int res = 0; + while (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; +} diff --git a/solution/0000-0099/0009.Palindrome Number/README.md b/solution/0000-0099/0009.Palindrome Number/README.md index 5293dccce090b..46fbfac16ef77 100644 --- a/solution/0000-0099/0009.Palindrome Number/README.md +++ b/solution/0000-0099/0009.Palindrome Number/README.md @@ -248,6 +248,24 @@ class Solution { } ``` +#### C + +```c +bool isPalindrome(int x) { + if (x < 0 || (x != 0 && x % 10 == 0)) { + return false; + } + + int y = 0; + while (y < x) { + y = y * 10 + x % 10; + x /= 10; + } + + return (x == y || x == y / 10); +} +``` + diff --git a/solution/0000-0099/0009.Palindrome Number/README_EN.md b/solution/0000-0099/0009.Palindrome Number/README_EN.md index 8b0ac2e383ca0..712ba430b9d5a 100644 --- a/solution/0000-0099/0009.Palindrome Number/README_EN.md +++ b/solution/0000-0099/0009.Palindrome Number/README_EN.md @@ -240,6 +240,24 @@ class Solution { } ``` +#### C + +```c +bool isPalindrome(int x) { + if (x < 0 || (x != 0 && x % 10 == 0)) { + return false; + } + + int y = 0; + while (y < x) { + y = y * 10 + x % 10; + x /= 10; + } + + return (x == y || x == y / 10); +} +``` + diff --git a/solution/0000-0099/0009.Palindrome Number/Solution.c b/solution/0000-0099/0009.Palindrome Number/Solution.c new file mode 100644 index 0000000000000..c13f8cc7947ca --- /dev/null +++ b/solution/0000-0099/0009.Palindrome Number/Solution.c @@ -0,0 +1,13 @@ +bool isPalindrome(int x) { + if (x < 0 || (x != 0 && x % 10 == 0)) { + return false; + } + + int y = 0; + while (y < x) { + y = y * 10 + x % 10; + x /= 10; + } + + return (x == y || x == y / 10); +}