Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaeb439c

Browse files
author
applewjg
committed
Longest Palindromic Substring
1 parent890a768 commitaeb439c

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

‎LongestPalindromicSubstring.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 13, 2014
4+
Problem: Longest Palindromic Substring
5+
Difficulty: Medium
6+
Source: https://oj.leetcode.com/problems/longest-palindromic-substring/
7+
Notes:
8+
Given a string S, find the longest palindromic substring in S.
9+
You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
10+
11+
Solution: 1. Time O(n^2), Space O(n^2)
12+
2. Time O(n^2), Space O(n)
13+
3. Time O(n^2), Space O(1) (actually much more efficient than 1 & 2)
14+
4. Time O(n), Space O(n) (Manacher's Algorithm)
15+
*/
16+
publicclassSolution {
17+
publicStringlongestPalindrome_1(Strings) {
18+
intn =s.length();
19+
boolean[][]dp =newboolean[n][n];
20+
intidx =0,maxLen =0;
21+
for (intk =0;k <n; ++k) {
22+
for (inti =0;i +k <n; ++i) {
23+
if (k ==0 ||k ==1)dp[i][i+k] = (s.charAt(i) ==s.charAt(i+k));
24+
elsedp[i][i+k] = (s.charAt(i) ==s.charAt(i+k)) ?dp[i+1][i+k-1] :false;
25+
if (dp[i][i+k] ==true && (k+1) >maxLen) {
26+
idx =i;maxLen =k +1;
27+
}
28+
}
29+
}
30+
returns.substring(idx,idx +maxLen);
31+
}
32+
publicStringlongestPalindrome_2(Strings) {
33+
intn =s.length();
34+
boolean[][]dp =newboolean[2][n];
35+
intidx =0,maxLen =0;
36+
intcur =1,last =0;
37+
for (inti =0;i <n; ++i) {
38+
cur =cur +last - (last =cur);
39+
for (intj =i;j >=0; --j) {
40+
if (j ==i ||j ==i -1)
41+
dp[cur][j] = (s.charAt(i) ==s.charAt(j));
42+
elsedp[cur][j] = (s.charAt(i) ==s.charAt(j)) &&dp[last][j +1];
43+
if (dp[cur][j] && (i -j +1) >maxLen) {
44+
idx =j;maxLen =i -j +1;
45+
}
46+
}
47+
}
48+
returns.substring(idx,idx +maxLen);
49+
}
50+
publicStringlongestPalindrome_3(Strings) {
51+
intn =s.length();
52+
intidx =0,maxLen =0;
53+
for (inti =0;i <n; ++i) {
54+
for (intj =0;j <=1; ++j) {
55+
booleanisP =true;
56+
for (intk =0;i -k >=0 &&i +j +k <n &&isP; ++k) {
57+
isP = (s.charAt(i -k) ==s.charAt(i +j +k));
58+
if (isP && (j +1 +k*2) >maxLen) {
59+
idx =i -k;maxLen =j +1 +k*2;
60+
}
61+
}
62+
}
63+
}
64+
returns.substring(idx,idx +maxLen);
65+
}
66+
publicStringlongestPalindrome_4(Strings) {
67+
intn =s.length();
68+
intidx =0,maxLen =0;
69+
StringBuffersb =newStringBuffer();
70+
sb.append('^');
71+
for (inti =0;i <n; ++i) {
72+
sb.append('#');
73+
sb.append(s.charAt(i));
74+
}
75+
sb.append("#$");
76+
n =2 *n +2;
77+
intmx =0,id =0;
78+
int[]p =newint[n];
79+
Arrays.fill(p,0);
80+
for (inti =1;i <n -1; ++i) {
81+
p[i] = (mx >i) ?Math.min(p[2 *id -i],mx -i):0;
82+
while (sb.charAt(i +1 +p[i]) ==sb.charAt(i -1 -p[i])) ++p[i];
83+
if (i +p[i] >mx) {
84+
id =i;mx =i +p[i];
85+
}
86+
if (p[i] >maxLen) {
87+
idx =i;maxLen =p[i];
88+
}
89+
}
90+
idx = (idx -maxLen -1) /2;
91+
returns.substring(idx,idx +maxLen);
92+
}
93+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp