Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
25 changes: 19 additions & 6 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,29 @@ Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: false
Comment thread
Zhongnibug marked this conversation as resolved.
Outdated
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: false
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
AlwaysBreakTemplateDeclarations: MultiLine
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: false
AfterEnum: false
Expand All @@ -39,6 +44,7 @@ BraceWrapping:
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
Expand Down Expand Up @@ -79,6 +85,7 @@ MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
Expand All @@ -87,20 +94,22 @@ PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
RawStringFormats:
- Delimiter: pb
Language: TextProto
BasedOnStyle: google
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
Expand All @@ -109,6 +118,10 @@ SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 8
UseTab: Never
...

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: `// TODO`
Comment thread
tiazahmd marked this conversation as resolved.
Outdated
* Space Complexity: `// TODO`
Comment thread
tiazahmd marked this conversation as resolved.
Outdated

### AC Result

`11509 / 11509 test cases passed`

| Status | Runtime | Memory |
|--------|---------|--------|
| Accepted | 36 ms | 11.7 MB |
78 changes: 78 additions & 0 deletions src/PalindromeNumber/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Solution by Imtiaz Ahmed (Github: tiazahmd)

#include <iostream>
#include <vector>

#include "../Test.h"

using namespace std;
using namespace leetcode;

class Solution {
public:
string isPalindrome(int x) {
Comment thread
tiazahmd marked this conversation as resolved.
Outdated
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;
Comment thread
tiazahmd marked this conversation as resolved.

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++) {
Comment thread
tiazahmd marked this conversation as resolved.
if (i == vec.size() - 1)
return "true";
else if (vec[i] != vec[vec.size() - 1 - i])
return "false";
}

} else {
return "false";
}

return "false";
};
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
};
}

};

int stringToInteger(string input) { return stoi(input); }

string boolToString(bool input) { return input ? "True" : "False"; }

int main() {

Solution s;

test("Test case: ", s.isPalindrome(121), "true");
test("Test case: ", s.isPalindrome(120), "true");
test("Test case: ", s.isPalindrome(-121), "true");
Comment thread
tiazahmd marked this conversation as resolved.
Outdated
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");

Comment thread
MrHeer marked this conversation as resolved.
Outdated
Comment thread
tiazahmd marked this conversation as resolved.
Outdated
// string line;
// while (getline(cin, line)) {
// int x = stringToInteger(line);

// bool ret = Solution().isPalindrome(x);

// string out = boolToString(ret);
// cout << out << endl;
// }
return 0;
}
Comment thread
tiazahmd marked this conversation as resolved.
Outdated
Comment thread
tiazahmd marked this conversation as resolved.
Outdated