【JAVA、C++】LeetCode 005 Longest Palindromic Substring
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.
解题思路一:
暴力枚举
共N^2量级个子串(从下标零开始),每次检查需一个for循环,等于是3重for循环,时间复杂度O(n^3)
解题思路二:
动态规划
设定一个表格table[][],其中table[i][j]表示substring(i,j+1)是不是Palindromic,时间复杂度为O(n^2)空间复杂度也为O(n^2)。
Java代码如下:
static public String longestPalindrome(String s) { if(s.length()==1)return s; int[][] table=new int[s.length()][s.length()]; int beginIndex = 0,endIndex = 0; //初始化第一、第二条斜线 for(int i=0;i<s.length();i++){ table[i][i]=1; if(i==s.length()-1)break; if(s.charAt(i)==s.charAt(i+1)){ table[i][i+1]=1; beginIndex=i; endIndex=i+1; } } //给第k条斜线赋值 for(int k=2;k<s.length();k++){ for(int i=0;i<s.length()-k;i++){ if(table[i+1][i+k-1]==1&&s.charAt(i)==s.charAt(i+k)){ table[i][i+k]=1; beginIndex=i; endIndex=i+k; } } } printTable(table); return s.substring(beginIndex,endIndex+1); } public static void printTable(int table[][]){ for(int i=0;i<table.length;i++){ for(int j=0;j<table[i].length;j++){ System.out.print(table[i][j]+" "); } System.out.println(""); } }
提交结果,TimeExceed。证明时间复杂度为O(n^2)是不能提交通过的。
解题思路三:
中心法,对S中每一个字符及重复的双字符为中心,进行遍历。时间复杂度为O(n^2),在leetcode上竟然Accepted!
Java代码如下:
static public String longestPalindrome(String s) { if (s.length() == 1) return s; String longest = s.substring(0, 1); for (int i = 0; i < s.length(); i++) { // 检查单字符中心 String tmp = helper(s, i, i); if (tmp.length() > longest.length()) longest = tmp; // 检查多字符中心 tmp = helper(s, i, i + 1); if (tmp.length() > longest.length()) longest = tmp; } return longest; } public static String helper(String s, int begin, int end) { while (begin >= 0 && end <= s.length() - 1 && s.charAt(begin) == s.charAt(end)) { begin--; end++; } return s.substring(begin + 1, end); }
解题思路四:
Manacher’s algorithm,时间复杂度为O(n)
算法思路比较复杂,参考链接
http://blog.csdn.net/ggggiqnypgjg/article/details/6645824
http://blog.csdn.net/xingyeyongheng/article/details/9310555
Java代码
static public String longestPalindrome(String s) { char[] sChar = new char[2 * s.length() + 1]; for (int i = 0; i < sChar.length; i++) { if (i % 2 == 0) sChar[i] = '#'; else sChar[i] = s.charAt(i / 2); } int[] p = new int[2 * s.length() + 1]; int id = 0, mx = 0, maxID = 0; for (int i = 0; i < sChar.length; i++) { // 核心算法 if (mx > i) { p[i] = Math.min(p[2 * id - i], mx - i); } else p[i] = 1; int low = i - p[i], high = i + p[i]; while (low >= 0 && high <= (sChar.length - 1)) { if (sChar[low] == sChar[high]) { p[i]++; low--; high++; } else break; } // 更新id和mx的值 if (i + p[i] > mx) { id = i; mx = id + p[i]; } // 更新取得最大p【i】的id if (p[maxID] < p[i]) maxID = i; } char[] result = new char[p[maxID] - 1]; for (int i = 0; i < result.length; i++) { result[i] = sChar[maxID - p[maxID] + 2 + 2 * i]; } return new String(result); }
C++实现如下:
1 #include<string> 2 #include<vector> 3 #include<algorithm> 4 using namespace std; 5 class Solution { 6 public: 7 string longestPalindrome(string s) { 8 vector<char> sChar(2 * s.length() + 1, '#'); 9 for (int i = 0; i < sChar.size(); i++) 10 if(i&1) 11 sChar[i]= s[i / 2]; 12 vector<int> p(2 * s.length() + 1,0); 13 int id = 0, mx = 0, maxID = 0; 14 for (int i = 0; i < sChar.size(); i++) { 15 // 核心算法 16 if (mx > i) { 17 int temp = p[2 * id - i]; 18 p[i] =min(temp, mx - i); 19 } 20 else 21 p[i] = 1; 22 int low = i - p[i], high = i + p[i]; 23 while (low >= 0 && high <= (sChar.size() - 1)) { 24 if (sChar[low] == sChar[high]) { 25 p[i]++; 26 low--; 27 high++; 28 } 29 else 30 break; 31 } 32 if (i + p[i] > mx) { 33 id = i; 34 mx = id + p[i]; 35 } 36 if (p[maxID] < p[i]) 37 maxID = i; 38 } 39 vector<char> result(p[maxID] - 1,'0'); 40 for (int i = 0; i < result.size(); i++) { 41 result[i] = sChar[maxID - p[maxID] + 2 + 2 * i]; 42 } 43 string res; 44 res.assign(result.begin(), result.end()); 45 return res; 46 } 47 };