8

#yyds干货盘点# 名企真题专题: 回文串

 1 year ago
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.
neoserver,ios ssh client

#yyds干货盘点# 名企真题专题: 回文串

精选 原创

97的风 2022-12-31 13:04:02 博主文章分类:面试题 ©著作权

文章标签 System 字符串 i++ 文章分类 Java 编程语言 阅读数186

1.简述:

描述

给定一个字符串,问是否能通过添加一个字母将其变为回文串。

输入描述:

一行一个由小写字母构成的字符串,字符串长度小于等于10。

输出描述:

输出答案(YES\NO).

示例1

2.代码实现:

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;
}
}
  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK