当前位置:网站首页>Three methods to judge whether it is palindrome structure
Three methods to judge whether it is palindrome structure
2022-07-27 06:43:00 【Xiangxiang baby:】
The first one is ( Transformation type )
We put the user input int Type data converted to String character string , Then in reverse order , Compare .
pubic static boolean isCircle(int x){
String revX = (new StringBuilder(x + ””).reverse().toString();
return (x + “”).equals(revx);
}
As for why to use x + ‘’’’,int Data of type +‘’‘’ Can be converted to String Data of type , Remember and get used to
The second kind ( First get the number of digits of the user input value , Find the leftmost and rightmost values , Compare whether the two are the same , If they are the same , Palindrome structure )
public static boolean isCircle(int x){
// If the value entered is less than 0, Definitely not palindrome structure
if(x < 0){
return false;
}
int div = 1;
// Number of Statistics
while(x/div >= 10){
div *= 10;
}
// Compare the left and right values one by one
while(x > 0){
int left = x /div;
int right = x % 10;
if(left != right){
return false;
}
x = (x % div)/10;
div /= 100;
}
return true;
}
}
The third kind of (manacher Algorithm , The algorithm is if it is palindrome structure , There must be the same second half and the first half )
public static boolean isCircle(int x){
//rex Newly generated data for the second half
int rex = 0;
//div Used for digit transformation
int div = 10;
// Let's experience it here , If the last number is 0, Definitely not palindrome structure ( How can the first number at the beginning be 0?
if(x == 0 || x%10 == 0 && x != 0 ){
return false;
}
//x The reason why it should be greater than rex, Because if it is a number of singular length , Leave the middle number to x Will compare successfully .
while(x > rex){
rex = res*10 + x%div;
div *= 10;
}
//x == rex/10, Because of x Maybe it's better than rex One less
return x == rex || x == rex/10;
}
If you still have any ideas , Leave a comment ~
边栏推荐
猜你喜欢
随机推荐
If conditional statement of shell
Linu性能调优:面对DDOS攻击,我们如何缓解局面?
备忘录 @RestControllerAdvice与异常拦截类示例
LVM与磁盘配额
Markdown文档常用字体及颜色设置
Open source WebGIS related knowledge
Compatibility test knowledge points
DHCP principle and configuration
keras-ocr实例测试
Installation, configuration and use of gradle
regular expression
shell的编程规范and重定向与管道操作
LVM and disk quota
正则表达式
The concept of interface testing and the use of postman tools
logging日志的封装
Packaging of logging logs
where接自定义函数导致查询缓慢
英语基础知识:修饰性的句子成分-上篇
Vscode solves the problem of using stuck ipynb files when running








