知识点
字符串,反转
解题思路
将字符串通过“.”分割成字符串数组split,遍历这个数组的字符串,这些字符串可能会有前导和后导0需要去掉。
去掉之后再通过反转这个字符串看是否反正之后与原来字符串相同,相同返回true,如果字符串数组全部字符都是反正字符串则返回true。
Java题解
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param x string字符串
* @return bool布尔型
*/
public boolean isPalindromeNumber (String x) {
// write code here
String[] split = x.split("\\.");
boolean ans = true;
for (String s : split) {
s = s.replaceFirst("^0+", ""); //去除前导0
s = s.replaceAll("0*$", ""); //去除后导0
ans = ans && isPalindrome(s);
}
return ans;
}
public boolean isPalindrome (String s) {
// write code here
StringBuilder sb1 = new StringBuilder(s);
sb1.reverse();
return sb1.toString().equals(s);
}
}



京公网安备 11010502036488号