File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed
Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ package LeetCode .String .HuiWen ;
2+ import java .util .ArrayList ;
3+ public class Solution {
4+ public String longestPalindrome (String s ) {
5+ int maxIndex = 0 ;
6+ ArrayList <String > str = new ArrayList <>();
7+ //求出所有子串(不去重)
8+ for (int i =0 ;i <s .length ()-1 ;i ++)
9+ for (int j =i +1 ;j <=s .length ();j ++) {
10+ str .add (s .substring (i , j ));//将字符串s的所有子串保存在str集合中
11+ }
12+ //求出所有回文子串(不去重)
13+ for (int m =0 ;m <str .size ();m ++)
14+ for (int i =0 ;i <((str .get (m )).length ())/2 ;i ++){
15+ if ((str .get (m )).charAt (i ) != (str .get (m )).charAt (((str .get (m )).length ())-i -1 )) {
16+ str .remove (m );//删除非回子串文串
17+ m --;
18+ break ;
19+ }
20+ }
21+ for (int i =1 ;i <str .size ();i ++) {
22+ if ((str .get (i )).length () > (str .get (maxIndex )).length ())
23+ maxIndex = i ;
24+ }
25+ return str .get (maxIndex );
26+ }
27+ }
28+
Original file line number Diff line number Diff line change 1+ package LeetCode .String .HuiWen ;
2+
3+ public class Test {
4+ public static void main (String [] args ) {
5+ Solution sol = new Solution ();
6+ System .out .println (sol .longestPalindrome ("babad" ));
7+ }
8+ }
You can’t perform that action at this time.
0 commit comments