Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6ddbb77

Browse files
committed
isPalindrome problem with two solutions
1 parent 165c57e commit 6ddbb77

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

‎README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,60 @@ var twoSum = function (nums, target) {
8181
}
8282
};
8383
```
84+
85+
### Given an integer x, return true if x is palindrome integer.
86+
87+
An integer is a palindrome when it reads the same backward as forward.
88+
89+
For example, 121 is a palindrome while 123 is not.
90+
91+
Example 1:
92+
93+
Input: x = 121
94+
Output: true
95+
Explanation: 121 reads as 121 from left to right and from right to left.
96+
Example 2:
97+
98+
Input: x = -121
99+
Output: false
100+
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
101+
Example 3:
102+
103+
Input: x = 10
104+
Output: false
105+
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
106+
107+
Constraints:
108+
109+
-231 <= x <= 231 - 1
110+
111+
### Solution 1
112+
113+
The space complexity for this solution is O(N).
114+
115+
The time complexity for this solution is O(N).
116+
117+
```js
118+
var isPalindrome = function (x) {
119+
let splitNumber = x.toString().split("");
120+
let reverseNumberArr = [];
121+
for (let i = splitNumber.length; i > -1; i--) {
122+
if (!isNaN(splitNumber[i])) {
123+
reverseNumberArr.push(splitNumber[i]);
124+
}
125+
}
126+
return reverseNumberArr.join("") === x;
127+
};
128+
```
129+
130+
### Solution 2
131+
132+
The space complexity for this solution is a constant O(1).
133+
134+
The time complexity for this solution is also O(1).
135+
136+
```js
137+
var isPalindrome = function (x) {
138+
return x.toString() === x.toString().split("").reverse().join("");
139+
};
140+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /