MCPcopy Create free account
hub / github.com/Blankj/awesome-java-leetcode / Solution

Class Solution

src/com/blankj/easy/_0009/Solution.java:11–42  ·  view source on GitHub ↗

author: Blankj blog : http://blankj.com time : 2017/04/24 desc :

Source from the content-addressed store, hash-verified

9 * </pre>
10 */
11public class Solution {
12// public boolean isPalindrome(int x) {
13// if (x < 0) return false;
14// int copyX = x, reverse = 0;
15// while (copyX > 0) {
16// reverse = reverse * 10 + copyX % 10;
17// copyX /= 10;
18// }
19// return x == reverse;
20// }
21
22 public boolean isPalindrome(int x) {
23 if (x < 0 || (x != 0 && x % 10 == 0)) return false;
24 int halfReverseX = 0;
25 while (x > halfReverseX) {
26 halfReverseX = halfReverseX * 10 + x % 10;
27 x /= 10;
28 }
29 return halfReverseX == x || halfReverseX / 10 == x;
30 }
31
32 public static void main(String[] args) {
33 Solution solution = new Solution();
34 System.out.println(solution.isPalindrome(-1));
35 System.out.println(solution.isPalindrome(10010));
36
37 System.out.println(solution.isPalindrome(0));
38 System.out.println(solution.isPalindrome(11));
39 System.out.println(solution.isPalindrome(111));
40 System.out.println(solution.isPalindrome(222222222));
41 }
42}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected