Movatterモバイル変換


[0]ホーム

URL:


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

Commit3a546a3

Browse files
add c++ solution for 9
1 parent2f3f186 commit3a546a3

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+
classSolution1 {
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+
boolisPalindrome(int x) {
31+
if (x ==0) {
32+
returntrue;
33+
}elseif (x <0) {
34+
returnfalse;
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+
classSolution2 {
48+
public:
49+
boolisPalindrome(int x) {
50+
if (x <0 || (x !=0 && x%10 ==0)) {
51+
returnfalse;
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)

[8]ページ先頭

©2009-2025 Movatter.jp