-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode5.java
More file actions
159 lines (143 loc) · 5.19 KB
/
LeetCode5.java
File metadata and controls
159 lines (143 loc) · 5.19 KB
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
154
155
156
157
158
159
import util.PrintUtil;
import java.util.HashMap;
public class LeetCode5 {
public static void main(String[] args) {
String S1, S2;
// 输入:s = "babad"
// 输出:"bab"
S1 = "babad";
System.out.println(S1);
S2 = new Solution5_3().longestPalindrome(S1);
System.out.println(S2);
PrintUtil.printDivider();
// 输入:s = "cbbd"
// 输出:"bb"
S1 = "cbbd";
System.out.println(S1);
S2 = new Solution5_3().longestPalindrome(S1);
System.out.println(S2);
PrintUtil.printDivider();
// 输入:s = "a"
// 输出:"a"
S1 = "a";
System.out.println(S1);
S2 = new Solution5_3().longestPalindrome(S1);
System.out.println(S2);
PrintUtil.printDivider();
// 输入:s =
// 输出:
S1 = "twbiqwtafgjbtolwprpdnymaatlbuacrmzzwhkpvuwdtyfjsbsqxrlxxtqkjlpkvpxmlajdmnyepsmczmmfdtjfbyybotpoebilayqzvqztqgddpcgpelwmriwmoeeilpetbxoyktizwcqeeivzgxacuotnlzutdowiudwuqnghjgoeyojikjhlmcsrctvnahnoapmkcrqmwixpbtirkasbyajenknuccojooxfwdeflmxoueasvuovcayisflogtpxtbvcxfmydjupwihnxlpuxxcclbhvutvvshcaikuedhyuksbwwjsnssizoedjkbybwndxpkwcdxaexagazztuiuxphxcedqstahmevkwlayktrubjypzpaiwexkwbxcrqhkpqevhxbyipkmlqmmmogrnexsztsbkvebjgybrolttvnidnntpgvsawgaobycfaaviljsvyuaanguhohsepbthgjyqkicyaxkytshqmtxhilcjxdpcbmvnpippdrpggyohwyswuydyrhczlxyyzregpvxyfwpzvmjuukswcgpenygmnfwdlryobeginxwqjhxtmbpnccwdaylhvtkgjpeyydkxcqarkwvrmwbxeetmhyoudfuuwxcviabkqyhrvxbjmqcqgjjepmalyppymatylhdrazxytixtwwqqqlrcusxyxzymrnryyernrxbgrphsioxrxhmxwzsytmhnosnrpwtphaunprdtbpwapgjjqcnykgspjsxslxztfsuflijbeebwyyowjzpsbjcdabxmxhtweppffglvhfloprfavduzbgkw";
System.out.println(S1);
S2 = new Solution5_3().longestPalindrome(S1);
System.out.println(S2);
PrintUtil.printDivider();
}
}
class Solution5_1 {
// NOTE: 时间复杂度还是高,根源在于搜索的顺序问题
HashMap<String, Boolean> map = new HashMap<String, Boolean>();
public boolean isPalindrome(String s, int start, int end) {
if (map.containsKey(start + "-" + end)) {
return map.get(start + "-" + end);
}
boolean res;
if (s.charAt(start) == s.charAt(end)) {
if (start + 1 == end || start + 2 == end) {
res = true;
} else {
res = isPalindrome(s, start + 1, end - 1);
}
} else {
res = false;
}
map.put(start + "-" + end, res);
return res;
}
public String longestPalindrome(String s) {
int start = 0;
int end = 0;
int length = 1;
for (int i = 0; i < s.length() - 1; i++) {
if (i + length >= s.length()) {
// 不可能存在满足长度的回文子串
continue;
}
for (int j = i + 1; j < s.length(); j++) {
if (j - i + 1 <= length) {
continue;
} else {
if (isPalindrome(s, i, j)) {
start = i;
end = j;
length = j - i + 1;
}
}
}
}
return s.substring(start, end + 1);
}
}
class Solution5_2 {
// NOTE: 遍历节点(中心展开)
public String longestPalindrome(String s) {
if (s == null || s.length() < 1) {
return "";
}
int start = 0, end = 0;
for (int i = 0; i < s.length(); i++) {
int len1 = expandAroundCenter(s, i, i);
int len2 = expandAroundCenter(s, i, i + 1);
int len = Math.max(len1, len2);
if (len > end - start) {
start = i - (len - 1) / 2;
end = i + len / 2;
}
}
return s.substring(start, end + 1);
}
public int expandAroundCenter(String s, int left, int right) {
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
--left;
++right;
}
return right - left - 1;
}
}
class Solution5_3 {
String result = "";
Boolean[][] memo;
public String longestPalindrome(String s) {
memo = new Boolean[s.length()][s.length()];
dp(0, s.length() - 1, s);
return this.result;
}
public boolean dp(int start, int end, String s) {
// System.out.println(start + " " + end);
if (memo[start][end] != null) {
return memo[start][end];
}
boolean res = false;
if (s.charAt(start) == s.charAt(end)) {
if (start == end || start + 1 == end || start + 2 == end) {
res = true;
} else {
if (dp(start + 1, end - 1, s)) {
res = true;
}
}
}
memo[start][end] = res;
if (res == false) {
dp(start, end - 1, s);
dp(start + 1, end, s);
} else {
if (end - start + 1 > this.result.length()) {
this.result = s.substring(start, end + 1);
}
}
return res;
}
}
class Solution5_4 {
// NOTE: 正向DP: 按照长度、开始点填表
}