-
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
Open
tiazahmd
wants to merge
21
commits into
XFreeCoder:master
Choose a base branch
from
tiazahmd:imtiaz-branch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
57ad31a
PalindromeNumber added - solution by Imtiaz
tiazahmd c41019a
feat: PalindromeNumber Solution Added
tiazahmd a1a3e29
Update src/PalindromeNumber/README.md
tiazahmd 555e5a6
fix: clang-format and source file
tiazahmd 4fd85b7
test: added test
tiazahmd 9853a55
fix: markdown
tiazahmd 7a2f091
fix: changes according to new feedback
tiazahmd b19deb3
Merge branch 'master' into imtiaz-branch
tiazahmd 57058aa
docs: updated master README and clang-format
tiazahmd cc4457f
Merge branch 'imtiaz-branch' of https://github.com/tiazahmd/LeetCode …
tiazahmd 7198fd7
fix: cleaned code and added new lines
tiazahmd 8ffedac
fix: clang-format file
tiazahmd 443769c
fix: clang-format file
tiazahmd 195013e
fix: clang-format file
tiazahmd fa23a6f
fix: clang-format file
tiazahmd 75dd4ff
fix: clang-format file
tiazahmd 1c71de1
fix: clang-format file
tiazahmd 33cd7de
fix: clang-format file
tiazahmd 37fe716
docs: readme updated
tiazahmd 0469310
docs: readme updated
tiazahmd 0898f01
Merge branch 'master' into imtiaz-branch
tiazahmd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# 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: `// TODO` | ||
tiazahmd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Space Complexity: `// TODO` | ||
tiazahmd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### AC Result | ||
|
||
`11509 / 11509 test cases passed` | ||
|
||
| Status | Runtime | Memory | | ||
|--------|---------|--------| | ||
| Accepted | 36 ms | 11.7 MB | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Solution by Imtiaz Ahmed (Github: tiazahmd) | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
bool isPalindrome(int x) { | ||
if (x >= 0 && x < 10) { | ||
return true; | ||
} | ||
else if (x >= 10) { | ||
vector<int> vec; | ||
long div = 10; | ||
long ct = 1; | ||
int num = 0; | ||
int digCount = 0; | ||
int temp = x; | ||
|
||
while (temp != 0) { | ||
temp /= 10; | ||
digCount++; | ||
} | ||
|
||
for (int i = 0; i < digCount; i++) { | ||
num = x % div; | ||
num /= ct; | ||
div *= 10; | ||
ct *= 10; | ||
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 stringToInteger(string input) { | ||
return stoi(input); | ||
} | ||
|
||
string boolToString(bool input) { | ||
return input ? "True" : "False"; | ||
} | ||
|
||
int main() { | ||
string line; | ||
while (getline(cin, line)) { | ||
int x = stringToInteger(line); | ||
|
||
bool ret = Solution().isPalindrome(x); | ||
|
||
string out = boolToString(ret); | ||
cout << out << endl; | ||
} | ||
return 0; | ||
} | ||
tiazahmd marked this conversation as resolved.
Show resolved
Hide resolved
tiazahmd marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.