Skip to content

Commit 3a546a3

Browse files
add c++ solution for 9
1 parent 2f3f186 commit 3a546a3

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ Your ideas/fixes/algorithms are more than welcome!
757757
|12|[Integer to Roman](https://leetcode.com/problems/integer-to-roman/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_12.java)|O(1)|O(1)||Medium| Math, String
758758
|11|[Container With Most Water](https://leetcode.com/problems/container-with-most-water/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_11.java)|O(n)|O(1)||Medium|
759759
|10|[Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_10.java)|O(m*n)|O(m*n)||Hard|DP
760-
|9|[Palindrome Number](https://leetcode.com/problems/palindrome-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_9.java)| O(n) | O(1) || Easy
760+
|9|[Palindrome Number](https://leetcode.com/problems/palindrome-number/)|[Java](../master/src/main/java/com/fishercoder/solutions/_9.java), [C++](../master/cpp/_9.cpp)| O(n) | O(1) || Easy
761761
|8|[String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_8.java)| O(n) | O(1) | |Medium
762762
|7|[Reverse Integer](https://leetcode.com/problems/reverse-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_7.java) | O(1) | O(1) | |Easy |
763763
|6|[ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_6.java) | O(n) | O(n) | |Easy |

cpp/_9.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* 9. Palindrome Number
3+
*
4+
* Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
5+
6+
Example 1:
7+
8+
Input: 121
9+
Output: true
10+
Example 2:
11+
12+
Input: -121
13+
Output: false
14+
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
15+
Example 3:
16+
17+
Input: 10
18+
Output: false
19+
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
20+
Follow up:
21+
22+
Coud you solve it without converting the integer to a string?
23+
*/
24+
25+
class Solution1 {
26+
/**This is a Java translation, while its Java counterpart could be accepted perfectly, this one failed due to
27+
* Char 21: runtime error: signed integer overflow: 746384741 * 10 cannot be represented in type 'int' (solution.cpp)
28+
*/
29+
public:
30+
bool isPalindrome(int x) {
31+
if (x == 0) {
32+
return true;
33+
} else if (x < 0) {
34+
return false;
35+
}
36+
int tmp = x;
37+
int reverse = 0;
38+
while (tmp != 0) {
39+
reverse *= 10;
40+
reverse += tmp % 10;
41+
tmp /= 10;
42+
}
43+
return reverse == x;
44+
}
45+
};
46+
47+
class Solution2 {
48+
public:
49+
bool isPalindrome(int x) {
50+
if (x < 0 || (x != 0 && x%10 == 0)) {
51+
return false;
52+
}
53+
int sum = 0;
54+
while (x > sum) {
55+
sum = sum*10 + x%10;
56+
x /= 10;
57+
}
58+
return (x == sum) || (x == sum/10);
59+
}
60+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy