Skip to content

Commit b5c71bb

Browse files
authored
Update 9. Palindrome Number.java
better solution for reference
1 parent dea919a commit b5c71bb

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

9. Palindrome Number.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,16 @@ public boolean isPalindrome(int x) {
1616
}
1717
}
1818
}
19+
20+
21+
// for ref: https://discuss.leetcode.com/topic/8090/9-line-accepted-java-code-without-the-need-of-handling-overflow
22+
23+
public boolean isPalindrome(int x) {
24+
if (x<0 || (x!=0 && x%10==0)) return false;
25+
int rev = 0;
26+
while (x>rev){
27+
rev = rev*10 + x%10;
28+
x = x/10;
29+
}
30+
return (x==rev || x==rev/10);
31+
}

0 commit comments

Comments
 (0)