diff --git a/README.md b/README.md index f6cc7d6..6b2b103 100644 --- a/README.md +++ b/README.md @@ -11,5 +11,6 @@ Code is easy to understand, documentation is pretty, and program performance is | Num | Problem | Solution | Time \| Space Complexity | Difficulty | |-----|---------|----------|--------------------------|------------| | 7 | [Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [Code](./src/ReverseInteger/Solution.cpp) | O(log(n)) \| O(1) | Easy | +| 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [Code](./src/PalindromeNumber/Solution.cpp) | O(log(n)) \| O(log(n)) | Easy | | 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [Code](./src/RomanToInteger/Solution.cpp) | O(n) \| O(1) | Easy | | 14 | [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/) | [Code](./src/LongestCommonPrefix/Solution.cpp) | O(n) \| O(n) | Easy | diff --git a/src/PalindromeNumber/README.md b/src/PalindromeNumber/README.md new file mode 100644 index 0000000..36cf024 --- /dev/null +++ b/src/PalindromeNumber/README.md @@ -0,0 +1,33 @@ +# Palindrome Number + +LeetCode [source](https://leetcode.com/problems/palindrome-number/) + +## Solution + +### Intuition + +The challenge with this problem is that you're not allowed to use any string. You have to take the integer and come up with a solution that doesn't employ string or any of its methods. From the first glance, what was apparent is that we'll need a data structure to hold the individual integers for comparison. + +### Algorithm + +Details of the the algorithm for the solution is as follows: + +1. If it's a single digit, then it's a Palindrome Number. +2. Ignore any negative integers. +3. For all other types of integers, pop each digit and store them in a vector in order. +4. Compare the **(n + i)th** integer with **(vector.size() - 1 - i)th** integer. +5. If this comparisos fails (i.e. **comparison == false**) return **false**. +6. Else continue the comparisons until **i** reaches the middle of the vector. If no **false** was return before, it means all the comparisons were **true** and therefore, the integer is a palindrome. + +### Complexity Analysis + +* Time Complexity: **O(log⁡(n))** +* Space Complexity: **O(log⁡(n))** + +### AC Result + +Test case result: **11509 / 11509 test cases passed** + +| Status | Runtime | Memory | +|--------|---------|--------| +| Accepted | 36 ms | 11.7 MB | diff --git a/src/PalindromeNumber/Solution.cpp b/src/PalindromeNumber/Solution.cpp new file mode 100644 index 0000000..fd1b07e --- /dev/null +++ b/src/PalindromeNumber/Solution.cpp @@ -0,0 +1,56 @@ +// Solution by Imtiaz Ahmed (Github: tiazahmd) + +#include +#include + +#include "../Test.h" + +using namespace std; +using namespace leetcode; + +class Solution { +public: + bool isPalindrome(int x) { + if (x >= 0 && x < 10) { + return true; + } else if (x >= 10) { + vector vec; + long div = 10; + int num = 0; + int temp = x; + + while (temp != 0) { + num = temp % div; + temp = temp / div; + vec.push_back(num); + } + + for (int i = 0; i < vec.size(); i++) { + if (i == vec.size() - 1) + return true; + else if (vec[i] != vec[vec.size() - 1 - i]) + return false; + } + + } else { + return false; + } + + return false; + }; +}; + +int main() { + + Solution s; + + test("Test case: ", s.isPalindrome(121), true); + test("Test case: ", s.isPalindrome(120), false); + test("Test case: ", s.isPalindrome(-121), false); + test("Test case: ", s.isPalindrome(11), true); + test("Test case: ", s.isPalindrome(9), true); + test("Test case: ", s.isPalindrome(0), true); + test("Test case: ", s.isPalindrome(10022001), true); + test("Test case: ", s.isPalindrome(1001), true); + return 0; +}