mirror of
https://github.com/Xevion/leetcode.git
synced 2025-12-06 07:15:22 -06:00
palindrome-number solution java
This commit is contained in:
27
palindrome-number/Solution.java
Normal file
27
palindrome-number/Solution.java
Normal file
@@ -0,0 +1,27 @@
|
||||
// Accepted
|
||||
// Runtime: 7ms
|
||||
// Memory Usage: 38.5 MB
|
||||
// Submitted: January 15th, 2021
|
||||
|
||||
class Solution {
|
||||
public boolean isPalindrome(int x) {
|
||||
if (x < 0)
|
||||
return false;
|
||||
else if (x >= 0 && x <= 9)
|
||||
return true;
|
||||
|
||||
byte[] digits = new byte[((int) Math.log10(x)) + 1];
|
||||
for (int i = 0; i < digits.length; i++) {
|
||||
digits[i] = (byte) (x % 10);
|
||||
x /= 10;
|
||||
}
|
||||
|
||||
int n = digits.length / 2;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (digits[i] != digits[digits.length - i - 1])
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user