给定一个字符串s,问该字符串里有多少个长度大于1的连续子串都是回文? 回文:正序的文本内容与倒序的文本内容相同,比如 aa,aba-笔试面试资料
这是qklbishe.com第6040 篇笔试面试资料
提供答案分析,通过本文《给定一个字符串s,问该字符串里有多少个长度大于1的连续子串都是回文? 回文:正序的文本内容与倒序的文本内容相同,比如 aa,aba-笔试面试资料》可以理解其中的代码原理,这是一篇很好的求职学习资料
本站提供程序员计算机面试经验学习,笔试经验,包括字节跳动/头条,腾讯,阿里,美团,滴滴出行,网易,百度,京东,小米,华为,微软等互联网大厂真题学习背诵。
答案:

public class Main {
public static int f(String s) {
int r = 0;
for (int i = 0; i < s.length(); i++) {
int a = i – 1;
int b = i + 1;
while (a >= 0 && b < s.length() && s.charAt(a) == s.charAt(b)) {
if (b – a – 1 > 1) {
r++;
}
a–;
b++;
}
if (b – a – 1 > 1) {
r++;
}
a = i;
b = i + 1;
while (a >= 0 && b < s.length() && s.charAt(a) == s.charAt(b)) {
if (b – a – 1 > 1) {
r++;
}
a–;
b++;
}
if (b – a – 1 > 1) {
r++;
}
}
return r;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
String s = scanner.nextLine();
System.out.println(f(s));
}
}
}
文章部分来自互联网,侵权联系删除
www.qklbishe.com
qklbishe.com区块链毕设代做网专注|以太坊fabric-计算机|java|毕业设计|代做平台 » 给定一个字符串s,问该字符串里有多少个长度大于1的连续子串都是回文? 回文:正序的文本内容与倒序的文本内容相同,比如 aa,aba-笔试面试资料