From 95d7f25175418638432bb14f20ceb521046fc8bc Mon Sep 17 00:00:00 2001 From: Keval Date: Sun, 27 Oct 2019 21:22:09 +0000 Subject: [PATCH 1/3] leetcode question 7 --- leetcode/src/7.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 leetcode/src/7.c diff --git a/leetcode/src/7.c b/leetcode/src/7.c new file mode 100644 index 00000000..1bc03434 --- /dev/null +++ b/leetcode/src/7.c @@ -0,0 +1,13 @@ +#include + +int reverse(int x){ + int rev = 0; + while (x != 0) { + int pop = x % 10; + x /= 10; + if (rev > INT_MAX/10 || (rev == INT_MAX / 10 && pop > 7)) return 0; + if (rev < INT_MIN/10 || (rev == INT_MIN / 10 && pop < -8)) return 0; + rev = rev * 10 + pop; + } + return rev; +} From af273adef07cd43ff9aefd75688875ccce2843d7 Mon Sep 17 00:00:00 2001 From: Keval Date: Sun, 27 Oct 2019 21:24:08 +0000 Subject: [PATCH 2/3] readme for leetcode-7 --- leetcode/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/leetcode/README.md b/leetcode/README.md index 7bbd3722..f6709006 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -10,6 +10,7 @@ LeetCode |2|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [C](./src/2.c)|Medium| |3|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [C](./src/3.c)|Medium| |4|[Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [C](./src/4.c)|Hard| +|7|[Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [C](./src/7.c)|Easy| |13|[Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [C](./src/13.c)|Easy| |20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [C](./src/20.c)|Easy| |21|[Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [C](./src/21.c)|Easy| From 138da16aafd682fa38ee3baade4d806f363808f4 Mon Sep 17 00:00:00 2001 From: Keval Date: Sun, 27 Oct 2019 21:57:49 +0000 Subject: [PATCH 3/3] leetcode 9 --- leetcode/README.md | 1 + leetcode/src/9.c | 13 +++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 leetcode/src/9.c diff --git a/leetcode/README.md b/leetcode/README.md index 7bbd3722..13aee908 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -10,6 +10,7 @@ LeetCode |2|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [C](./src/2.c)|Medium| |3|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [C](./src/3.c)|Medium| |4|[Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [C](./src/4.c)|Hard| +|9|[Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [C](./src/9.c)|Easy| |13|[Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [C](./src/13.c)|Easy| |20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [C](./src/20.c)|Easy| |21|[Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [C](./src/21.c)|Easy| diff --git a/leetcode/src/9.c b/leetcode/src/9.c new file mode 100644 index 00000000..28d05a0c --- /dev/null +++ b/leetcode/src/9.c @@ -0,0 +1,13 @@ +bool isPalindrome(int x){ + if(x < 0 || (x % 10 == 0 && x != 0)) { + return false; + } + + int revertedNumber = 0; + while(x > revertedNumber) { + revertedNumber = revertedNumber * 10 + x % 10; + x /= 10; + } + + return x == revertedNumber || x == revertedNumber/10; +}