Skip to content

Commit 7085a71

Browse files
author
刘勋
committed
回文子串
1 parent 21dc970 commit 7085a71

2 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.algorithm.study.demo.algorithm.leetcode;
2+
3+
import com.alibaba.fastjson.JSON;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/**
9+
* @author xun2.liu
10+
* @title: Solution10
11+
* @projectName algorithm-study
12+
* @description: 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串。
13+
*
14+
* 具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被计为是不同的子串。
15+
*
16+
* 示例 1:
17+
*
18+
* 输入: "abc"
19+
* 输出: 3
20+
* 解释: 三个回文子串: "a", "b", "c".
21+
* 示例 2:
22+
*
23+
* 输入: "aaa"
24+
* 输出: 6
25+
* 说明: 6个回文子串: "a", "a", "a", "aa", "aa", "aaa".
26+
*
27+
* 来源:力扣(LeetCode)
28+
* 链接:https://leetcode-cn.com/problems/palindromic-substrings
29+
* @date 2020/5/21 15:34
30+
*/
31+
public class Solution10 {
32+
//通过中心扩散法查找回文字符串
33+
public static void huiwen(String s,int l,int r,List<String> filter){
34+
while(l>=0 && r<s.length() && s.charAt(l)==s.charAt(r)){
35+
l--;r++;
36+
filter.add(s.substring(l+1,r));
37+
}
38+
}
39+
40+
public static int countSubstrings(String s) {
41+
if(null==s || s.length()==0){
42+
return 0;
43+
}
44+
if(s.length()==1){
45+
return 1;
46+
}
47+
List<String> filter=new ArrayList<String>();
48+
for(int i=0;i<s.length();i++){
49+
huiwen(s,i,i,filter);
50+
huiwen(s,i,i+1,filter);
51+
}
52+
return filter.size();
53+
}
54+
55+
public static void main(String[] args) {
56+
System.out.println(countSubstrings("aaa"));
57+
}
58+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.algorithm.study.demo.algorithm.leetcode;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
5+
/**
6+
* @author xun2.liu
7+
* @title: Solution9
8+
* @projectName algorithm-study
9+
* @description: 给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。
10+
*
11+
* 示例 1:
12+
*
13+
* 输入: "babad"
14+
* 输出: "bab"
15+
* 注意: "aba" 也是一个有效答案。
16+
* 示例 2:
17+
*
18+
* 输入: "cbbd"
19+
* 输出: "bb"
20+
*
21+
* 来源:力扣(LeetCode)
22+
* 链接:https://leetcode-cn.com/problems/longest-palindromic-substring
23+
* @date 2020/5/21 13:46
24+
*/
25+
public class Solution9 {
26+
/**
27+
* 通过中心点向两边扩散。
28+
* @param s
29+
* @param r
30+
* @param l
31+
* @return
32+
*/
33+
public static String palindrome(String s,int l,int r){
34+
while (l>=0 && r<s.length() && s.charAt(r)==s.charAt(l)){
35+
l--;r++;
36+
}
37+
//截取l-r中间的字符
38+
return s.substring(l+1,r);
39+
}
40+
public static String longestPalindrome(String s) {
41+
if (StringUtils.isBlank(s) || s.length()==1){
42+
return s;
43+
}
44+
String res="";
45+
for (int i=0;i<s.length()-1;i++){
46+
//判断aba汇文字符串
47+
String p1 = palindrome(s, i, i);
48+
//判断aa回文字符串
49+
String p2 = palindrome(s, i, i+1);
50+
System.out.println(p1);
51+
res=res.length()>=p1.length()?res:p1;
52+
res=res.length()>=p2.length()?res:p2;
53+
}
54+
return res;
55+
}
56+
public static void main(String[] args) {
57+
System.out.println(longestPalindrome("abc"));
58+
}
59+
}

0 commit comments

Comments
 (0)