-
Notifications
You must be signed in to change notification settings - Fork 0
/
PalindromeNumber.java
38 lines (34 loc) · 959 Bytes
/
PalindromeNumber.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* @author Shogo Akiyama
* Solved on 08/18/2019
*
* 9. Palindrome Number
* https://leetcode.com/problems/palindrome-number/
* Difficulty: Easy
*
* To run the code in LeetCode, take the codes from the following method(s):
* - boolean isPalindrome(int x)
* - boolean check(char[] arr, int i, int j).
*
* Runtime: 8 ms, faster than 40.58% of Java online submissions for Palindrome Number.
* Memory Usage: 36.7 MB, less than 5.02% of Java online submissions for Palindrome Number.
*
*/
public class PalindromeNumber {
public boolean isPalindrome(int x) {
if(x < 0){
return false;
}
char[] array = ("" + x).toCharArray();
return check(array, 0, array.length-1);
}
private boolean check(char[] arr, int i, int j){
if(i >= j){
return true;
}
if(arr[i] == arr[j]){
return check(arr, ++i, --j);
}
return false;
}
}