-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: master
Are you sure you want to change the base?
Changes from all commits
57ad31a
c41019a
a1a3e29
555e5a6
4fd85b7
9853a55
7a2f091
b19deb3
57058aa
cc4457f
7198fd7
8ffedac
443769c
195013e
fa23a6f
75dd4ff
1c71de1
33cd7de
37fe716
0469310
0898f01
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
return 0; | ||||||||||||||||||||||||||||||||||||||||||
} |
Uh oh!
There was an error while loading. Please reload this page.