8
#yyds干货盘点# 名企真题专题: 回文串
source link: https://blog.51cto.com/u_15488507/5982105
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
#yyds干货盘点# 名企真题专题: 回文串
精选 原创1.简述:
描述给定一个字符串,问是否能通过添加一个字母将其变为回文串。
输入描述:一行一个由小写字母构成的字符串,字符串长度小于等于10。
输出描述:输出答案(YES\NO).
示例12.代码实现:
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String str ;
boolean re=false;
String op="";
while(input.hasNext()){
str = input.next();
re = false;
for(int i=0;i<str.length();i++){
if(i==0){
op = str.substring(1,str.length());
}else{
if(i == str.length()-1){
op = str.substring(0,str.length()-1);
}else{
op = str.substring(0,i)+str.substring(i+1,str.length());
}
}
re = panduan(op);
if(re){
System.out.println("YES");
break;
}
}
if(re == false){System.out.println("NO");}
}
}
public static boolean panduan(String s){
char[] list = s.toCharArray();
int i=0,j=list.length-1;
while(i<j){
if(list[i] != list[j]){
return false;
}
i++;
j--;
}
return true;
}
}
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String str ;
boolean re=false;
String op="";
while(input.hasNext()){
str = input.next();
re = false;
for(int i=0;i<str.length();i++){
if(i==0){
op = str.substring(1,str.length());
}else{
if(i == str.length()-1){
op = str.substring(0,str.length()-1);
}else{
op = str.substring(0,i)+str.substring(i+1,str.length());
}
}
re = panduan(op);
if(re){
System.out.println("YES");
break;
}
}
if(re == false){System.out.println("NO");}
}
}
public static boolean panduan(String s){
char[] list = s.toCharArray();
int i=0,j=list.length-1;
while(i<j){
if(list[i] != list[j]){
return false;
}
i++;
j--;
}
return true;
}
}
- 赞
- 收藏
- 评论
- 分享
- 举报
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK