You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -658,7 +658,7 @@ Your ideas/fixes/algorithms are more than welcome!
658
658
|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|
659
659
|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|
|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
* Determine whether an integer is a palindrome. Do this without extra space.
5
-
* <p>
7
+
*
6
8
* Some hints:
9
+
*
7
10
* Could negative integers be palindromes? (ie, -1)
8
-
* <p>
11
+
*
9
12
* If you are thinking of converting the integer to string, note the restriction of using extra space.
10
-
* <p>
13
+
*
11
14
* You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
12
-
* <p>
15
+
*
13
16
* There is a more generic way of solving this problem.
14
17
*/
15
18
publicclass_9 {
16
-
17
-
/**Purely my original solution: just reverse the entire number and compare with itself, return if they two are equal or not.*/
18
-
publicbooleanisPalindrome(intx) {
19
-
if (x == 0) {
20
-
returntrue;
21
-
}
22
-
if (x < 0) {
23
-
returnfalse;
24
-
}
25
-
intrev = 0;
26
-
inttmp = x;
27
-
while (tmp != 0) {
28
-
rev *= 10;
29
-
rev += tmp % 10;
30
-
tmp /= 10;
19
+
20
+
publicstaticclassSolution1 {
21
+
publicbooleanisPalindrome(intx) {
22
+
if (x == 0) {
23
+
returntrue;
24
+
}
25
+
if (x < 0) {
26
+
returnfalse;
27
+
}
28
+
intrev = 0;
29
+
inttmp = x;
30
+
while (tmp != 0) {
31
+
rev *= 10;
32
+
rev += tmp % 10;
33
+
tmp /= 10;
34
+
}
35
+
returnrev == x;
31
36
}
32
-
returnrev == x;
33
37
}
34
38
35
-
/**Then I turned to Discuss and found a more efficient way: reversing only half and then compare if they're equal.*/
0 commit comments