题解LeetCode——回文数
我的LeetCode代码集:https://github.com/cnamep001/LeetCode
原题链接:https://leetcode-cn.com/problems/palindrome-number/description/
题目描述:
思路一:翻转数字,判断是否和原数字相等
翻转过程中注意越界的情况,如果越界,直接返回false。事实上如果翻转之后越界了,肯定就不会和原数字相等了,我们在代码实现时无需考虑这种情况。
翻转过程:
设置一个int类型的reverse,其初值为0,只要x > 0,就继续循环, 每一轮循环执行reverse = reverse * 10 + x % 10;和x /= 10;的操作。当然一开始我们要先设一个变量记录x的原值。
此方法的时间复杂度是O(n)级别的,其中n为整数的位数。空间复杂度是O(1)。
package com.m.palindrome_number.solution4;
public class Solution4 {
public boolean isPalindrome(int x) {
if (x < 0) {
return false;
}
if (x <= 9) {
return true;
}
int reverse = 0;
int temp = x;
while (temp > 0) {
reverse = reverse * 10 + temp % 10;
temp = temp / 10;
}
return reverse == x;
}
}
LeetCode解题报告:
测试代码:
package com.m.palindrome_number.solution4;
public class Test4 {
public static void main(String[] args) {
int [] arr = new int[]{6, 2, 6, 6, 2, 6};
Solution4 solution4 = new Solution4();
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i <arr.length ; i++) {
stringBuilder.append(arr[i]);
}
System.out.println(solution4.isPalindrome(Integer.parseInt(stringBuilder.toString()))); //true
}
}
思路二:利用一个ArrayList存储数字中每一位的值
此方法的时间复杂度是O(n)级别的,其中n为整数的位数。空间复杂度也是O(n)。
实现代码:
package com.m.palindrome_number.solution2;
import java.util.ArrayList;
public class Solution2 {
public boolean isPalindrome(int x) {
if (x < 0) {
return false;
}
if (x == 0) {
return true;
}
ArrayList<Character> arrayList = new ArrayList<>();
while (x > 0) {
arrayList.add((char) (x % 10));
x /= 10;
}
for (int i = 0; i <= arrayList.size() / 2; i++) {
if (arrayList.get(i) != arrayList.get(arrayList.size() - 1 - i)) {
return false;
}
}
return true;
}
}
LeetCode解题报告:
测试代码:
package com.m.palindrome_number.solution2;
public class Test2 {
public static void main(String[] args) {
int [] arr = new int[]{2, 1, 2, 2, 1, 2};
Solution2 solution2 = new Solution2();
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i <arr.length ; i++) {
stringBuilder.append(arr[i]);
}
System.out.println(solution2.isPalindrome(Integer.parseInt(stringBuilder.toString()))); //true
}
}
思路三:依次比较数字的最高位和最低位是否相等
此方法的时间复杂度是O(n)级别的,其中n为整数的位数。空间复杂度是O(1)。
实现代码:
package com.m.palindrome_number.solution3;
public class Solution3 {
public boolean isPalindrome(int x) {
if(x < 0) {
return false;
}
if(x <= 9) {
return true;
}
int num = 0;
int temp = x;
while(temp > 0) {
temp /= 10;
num++;
}
while(num > 1) {
int left = x / (int)(Math.pow(10, num - 1));
int right = x % 10;
if(left != right) {
return false;
}
x = (x - right - left * (int)(Math.pow(10, num - 1))) / 10;
num -= 2;
}
return true;
}
}
LeetCode解题报告:
测试代码:
package com.m.palindrome_number.solution3;
public class Test3 {
public static void main(String[] args) {
int [] arr = new int[]{5, 1, 5, 5, 1, 5};
Solution3 solution3 = new Solution3();
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i <arr.length ; i++) {
stringBuilder.append(arr[i]);
}
System.out.println(solution3.isPalindrome(Integer.parseInt(stringBuilder.toString()))); //true
}
}