-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestPalindromicStr.java
153 lines (135 loc) · 6.09 KB
/
LongestPalindromicStr.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
class LongestPalindromicStr {
// 找出最长回文子串
public String longestPalindrome_bak(String s) {
if (s.length() <= 1) {
return s;
}
char[] charArr = s.toCharArray();
int maxSize = 0;
String resStr = "";
for (int cur = 1; cur < charArr.length - maxSize / 2; cur++) {
int left = cur, right = cur;
int size = 0;
boolean flag1 = false;
boolean flag2 = false;
while (!flag1 && !flag2 && charArr[left] == charArr[right]) {
//多个重叠中间情况的处理
if (cur == left) {
while(charArr[cur] == charArr[right]) {
right++;
if (right == charArr.length) {
break;
}
}
}
left--;
if (right < charArr.length) {
right++;
}
if (left < 0) {
flag1 = true;
}
if (right >= charArr.length) {
flag2 = true;
}
}
//判断退出时的状态
//1. 左右不等,但未到左右边界
if (flag1 == flag2 == false) {
left++;
//不需要right--,因为substring不包含右边界
}
//2. 左右相等,但到左边界或右边界
if (flag2) {
//到右边界
if (flag1) {
left = 0;
}else {
left++;
}
}
size = right - left + 1;
if (size > maxSize) {
maxSize = size;
resStr = s.substring(left, right);
}
}
return resStr;
}
public String longestPalindrome_p(String s) {
if (s.length() <= 1) {
return s;
}
String resultStr = "";
//List<String> palList = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
for (int j = s.length(); j >i; j--) {
String subStr = s.substring(i, j);
//判断subStr是否是回文子串
boolean isPal = true;
char[] charArr = subStr.toCharArray();
/**
if (charArr[0] == charArr[subStr.length()-1]) {
if (palList.contains(subStr.substring(1, subStr.length()))) {
palList.add(subStr);
}
}
*/
for (int k = 0, l = subStr.length()-1; k<l; k++, l--) {
if (charArr[k] != charArr[l]) {
isPal = false;
break;
}
}
if (isPal && subStr.length() > resultStr.length()) {
resultStr = subStr;
}
}
/**
for (String ss : palList) {
resultStr = ss.length() > resultStr.length() ? ss : resultStr;
}
*/
}
return resultStr;
}
public String longestPalindrome(String s) {
if (s.isEmpty()) {
return s;
}
int n = s.length();
boolean[][] dp = new boolean[n][n];
int left = 0;
int right = 0;
for (int i = n - 2; i >= 0; i--) {
dp[i][i] = true;
for (int j = i + 1; j < n; j++) {
dp[i][j] = s.charAt(i) == s.charAt(j) &&( j-i<3||dp[i+1][j-1]);//小于3是因为aba一定是回文
if(dp[i][j]&&right-left<j-i){
left=i;
right=j;
}
}
}
return s.substring(left,right+1);
}
public static void main(String[] args) {
LongestPalindromicStr l = new LongestPalindromicStr();
System.out.println(l.longestPalindrome("c")); //c
System.out.println(l.longestPalindrome("ac")); //a
System.out.println(l.longestPalindrome("")); //
System.out.println(l.longestPalindrome("babad")); //bab
System.out.println(l.longestPalindrome("cbbd")); //bb
System.out.println(l.longestPalindrome("cbbbbd")); //bbbb
System.out.println(l.longestPalindrome("abababa")); //abababa
System.out.println(l.longestPalindrome("abababab")); //abababa
System.out.println(l.longestPalindrome("cbbb")); //bbb
System.out.println(l.longestPalindrome("bbb")); //bbb
System.out.println(l.longestPalindrome("bbbaaaaaa")); //aaaaaa
System.out.println(l.longestPalindrome("bbba")); //bbb
System.out.println(l.longestPalindrome("abb")); //bb
System.out.println(l.longestPalindrome("aaaabaaa")); //aaabaaa
System.out.println(l.longestPalindrome("aba")); //aba
System.out.println(l.longestPalindrome("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg"));
}
}