题解 | #牛的回文编号II#
牛的回文编号II
https://www.nowcoder.com/practice/0b576fd673834425878b99c736bb6c34
考察判断回文情况下的字符串的相关操作。因为不要前导和后导0,又加上是字符串,所以可以使用replaAll结合正则表达式去除前导和后导0,然后根据小数点划分字符后进行回文判断。
具体Java代码如下所示
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param x string字符串
* @return bool布尔型
*/
public boolean isPalindromeNumber (String x) {
// write code here
x = x.replaceAll("^(0+)", "").replaceAll("0+$", ""); //去掉前导和后导0
String[] s = x.split("\\.");
String s1 = s[0];
String s2 = s[1];
return isPalind(s1)&&isPalind(s2);
}
public boolean isPalind(String x){
return x.equals(new StringBuilder(x).reverse().toString());
}
}

