摘要:是左閉右開區(qū)間,所以要。,要理解是和之間只有一個元素。循環(huán)每次的時候,都要更新子串更大的情況。補一種中點延展的方法循環(huán)字符串的每個字符,以該字符為中心,若兩邊為回文,則向兩邊繼續(xù)延展。循環(huán)返回長度最長的回文串即可。
Problem
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
ExampleGiven the string = "abcdzdcab", return "cdzdc".
ChallengeO(n2) time is acceptable. Can you do it in O(n) time.
Note動規(guī)好題。
.substring(start, end)是左閉右開區(qū)間,所以end要+1。
if (s.charAt(i) == s.charAt(j) && (i - j <= 2 || dp[j+1][i-1])),要理解i - j <= 2是i和j之間只有一個元素。
循環(huán)每次i - j > end - start的時候,都要更新子串更大的情況。Time和space都是O(n^2)。
補一種中點延展的方法:循環(huán)字符串的每個字符,以該字符為中心,若兩邊為回文,則向兩邊繼續(xù)延展。如此,每個字符必對應一個最長回文串。循環(huán)返回長度最長的回文串即可。
Solution DPpublic class Solution { public String longestPalindrome(String s) { // Write your code here int len = s.length(); boolean[][] dp = new boolean[len][len]; int start = 0, end = 0; for (int i = 0; i < len; i++) { for (int j = 0; j <= i; j++) { if (s.charAt(i) == s.charAt(j) && (i - j <= 2 || dp[j+1][i-1])) { dp[j][i] = true; if (end - start < i - j ) { start = j; end = i; } } } } return s.substring(start, end+1); } }方法二:中點延展
public class Solution { String longest = ""; public String longestPalindrome(String s) { for (int i = 0; i < s.length(); i++) { helper(s, i, 0); helper(s, i, 1); } return longest; } public void helper(String s, int i, int os) { int left = i, right = i + os; while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) { left--; right++; } String cur = s.substring(left+1, right); if (cur.length() > longest.length()) { longest = cur; } } }2018-02-03 Added some comments, same thoughts as solution 2
class Solution { public String longestPalindrome(String s) { if (s == null || s.length() < 2) return s; //return as result String longest = s.substring(0, 1); for (int i = 0; i < s.length()-1; i++) { //get "ABA" type palindrome String cur = getPalindrome(s, i, i); //get "ABBA" type palindrome, and compare its length with "ABA" type if (s.charAt(i+1)==s.charAt(i)) { String temp = getPalindrome(s, i, i+1); cur = cur.length() > temp.length() ? cur : temp; } //update longest with cur longest = longest.length() > cur.length() ? longest : cur; } return longest; } public String getPalindrome(String s, int left, int right) { while (left > 0 && right < s.length()-1 && s.charAt(left-1) == s.charAt(right+1)) { left--; right++; } //careful with the right bound return s.substring(left, right+1); } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉載請注明本文地址:http://systransis.cn/yun/65518.html
摘要:暴力算法就是找到所有每個都進行的檢查。時間復雜度是個調用平均時長為這里唯一確定用的是頭尾表示。因為的對稱性,我們可以從中間出發(fā)向兩邊延展,找到最長的分為兩種基本情況。奇數(shù)長度出發(fā)點一致,都為偶數(shù)長度出發(fā)點為相鄰的點,和結束是 Given a string s, find the longest palindromic substring in s. You may assume tha...
摘要:先排序,然后用數(shù)組記錄每一位上連續(xù)序列的長度,每次循環(huán)更新最大值存為。 Problem Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Clarification Your algorithm should run in O(n) com...
摘要:查找字符串最長回文思路回文有奇回文和偶回文,是奇回文,是偶回文回文都是中心對稱,找到對稱點后,同時向前后尋找回文的最長串即可奇回文和偶回文可以歸為同一種情況,即以為對稱點,以為對稱點,但為了代碼可讀性,可以分開討論代碼奇回文偶回文本題 查找字符串最長回文 Longest Palindromic Substring Given a string s, find the longest ...
摘要:自己沒事刷的一些的題目,若有更好的解法,希望能夠一起探討項目地址 自己沒事刷的一些LeetCode的題目,若有更好的解法,希望能夠一起探討 Number Problem Solution Difficulty 204 Count Primes JavaScript Easy 202 Happy Number JavaScript Easy 190 Reverse Bi...
摘要:第一個分割點第二個分割點第三個分割點 Problem Given a string containing only digits, restore it by returning all possible valid IP address combinations. Example Given 25525511135, return [ 255.255.11.135, 255....
閱讀 1367·2021-11-15 11:45
閱讀 3134·2021-09-27 13:36
閱讀 2878·2019-08-30 15:54
閱讀 995·2019-08-29 12:38
閱讀 2915·2019-08-29 11:22
閱讀 2996·2019-08-26 13:52
閱讀 2042·2019-08-26 13:30
閱讀 595·2019-08-26 10:37