Skip to content

feat: PalindromeNumber Solution Added #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
33 changes: 33 additions & 0 deletions src/PalindromeNumber/README.md
Original file line number Diff line number Diff line change
@@ -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 |
56 changes: 56 additions & 0 deletions src/PalindromeNumber/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Solution by Imtiaz Ahmed (Github: tiazahmd)

#include <iostream>
#include <vector>

#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<int> 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;
Comment on lines +28 to +39
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tiazahmd Do you think this is okay, I have tested AC on leetcode.

Suggested change
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;
for (int i = 0; i < vec.size() / 2; i++) {
if (vec[i] != vec[vec.size() - 1 - i])
return false;
}
return true;
} else {
return false;
}

};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
};
}

};

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);
Comment on lines +47 to +54
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);
test("Test case 1", s.isPalindrome(121), true);
test("Test case 2", s.isPalindrome(120), false);
test("Test case 3", s.isPalindrome(-121), false);
test("Test case 4", s.isPalindrome(11), true);
test("Test case 5", s.isPalindrome(9), true);
test("Test case 6", s.isPalindrome(0), true);
test("Test case 7", s.isPalindrome(10022001), true);
test("Test case 8", s.isPalindrome(1001), true);

return 0;
}