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

Commit4adeaf1

Browse files
committed
add 5 longest palindromic sub string
1 parent8cf1b4b commit4adeaf1

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Key: see inline comments. O(N^2)
3+
*
4+
*@param {string} s
5+
*@return {string}
6+
*/
7+
varlongestPalindrome=function(s){
8+
if(!s)returns;
9+
varlongest=s.substring(0,1);
10+
for(vari=0;i<s.length;i++){
11+
// odd
12+
vartmp=getPalindromeSubstring(s,i,i);
13+
if(tmp.length>longest.length){
14+
longest=tmp;
15+
}
16+
17+
// even, get longest palindrome with center of i, i+1
18+
tmp=getPalindromeSubstring(s,i,i+1);
19+
if(tmp.length>longest.length){
20+
longest=tmp;
21+
}
22+
}
23+
24+
returnlongest;
25+
};
26+
27+
// Given a center, either one letter or two letter,
28+
// Find longest palindrome
29+
vargetPalindromeSubstring=function(s,i,j){
30+
while(i>=0&&j<s.length&&s[i]===s[j]){
31+
i--;
32+
j++;
33+
}
34+
35+
returns.substring(i+1,j);
36+
};

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
*[1. Two Sum](https://leetcode.com/problems/two-sum/) -[Solution](./Medium/1-twoSum.js)
6767
*[2. Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) -[Solution](./Medium/2-addTwoNumbers.js)
6868
*[3. Longest Substring Without Repeating Characters](https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/) -[Solution](./Medium/3-lengthOfLongestSubstring.js)
69+
*[5. Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/) -[Solution](./Medium/5-longestPalindromicSubstring.js)
6970
*[11. Container With Most Water](https://leetcode.com/problems/container-with-most-water/) -[Solution](./Medium/11-containerMostWater.js)
7071
*[15. 3Sum](https://leetcode.com/problems/3sum/) -[Solution](./Medium/15-3sum.js)
7172
*[22. Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) -[Solution](./Medium/22-generateParentheses.js)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp