本站提供程序员计算机面试经验学习,笔试经验,包括字节跳动/头条,腾讯,阿里,美团,滴滴出行,网易,百度,京东,小米,华为,微软等互联网大厂真题学习背诵。
答案:
这是qklbishe.com第6389 篇笔试面试资料
提供答案分析,通过本文《给定一个字符串,返回这个字符串中有多少个回文子串。 两个相同的回文子串出现在不同的位置,认为是2个回文子串。 a、aa、aaa、aba、aabaa、abcba均认为是回文子串。-笔试面试资料》可以理解其中的代码原理,这是一篇很好的求职学习资料
本站提供程序员计算机面试经验学习,笔试经验,包括字节跳动/头条,腾讯,阿里,美团,滴滴出行,网易,百度,京东,小米,华为,微软等互联网大厂真题学习背诵。
答案:
给定一个字符串,返回这个字符串中有多少个回文子串。
两个相同的回文子串出现在不同的位置,认为是2个回文子串。
a、aa、aaa、aba、aabaa、abcba均认为是回文子串。

public static Integer palindromeNum(String str) { int num = 0; if (StringUtils.isNotBlank(str)) { int index = 0; int nextIndex; while (index < str.length()) { nextIndex = str.indexOf(str.charAt(index), index + 1); if (nextIndex == -1 || !isPalindrome(str.substring(index, nextIndex + 1))) { index++; continue; } else { num++; } index = nextIndex; } } return num; } public static Boolean isPalindrome(String str) { if (StringUtils.isNotBlank(str) && str.length() > 1) { char[] chars = str.toCharArray(); for (int i = 0; i < str.length() / 2; i++) { if (chars[i] != chars[str.length() - i - 1]) { return false; } } return true; } return false; }
今天 11:41:59 回复(0)
文章部分来自互联网,侵权联系删除
www.qklbishe.com