Leetcode: Palindrome Partition I II
生活随笔
收集整理的這篇文章主要介紹了
Leetcode: Palindrome Partition I II
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?
題目一, 題目二
?
?
思路
1. 第一遍做時就參考別人的, 現(xiàn)在又忘記了?做的時候使用的是二維動態(tài)規(guī)劃, 超時加超內(nèi)存
?
2. 只當(dāng) string 左部分是回文的時候才有可能減少 cut
?
3. 一維動規(guī). 令 cuts[i] 表示string[i, string.size()] 所需的切割數(shù), 那么
狀態(tài)轉(zhuǎn)移方程為 cuts[i] = min(cuts[j]+1) j > i && string[i, j] is palindrome
時間復(fù)雜度上仍是 o(n*n), 但更新 cuts 的限制條件比較多了, cuts[i] 更新頻率較低?
?
代碼:
超時二維動規(guī)代碼
#include <iostream> #include <memory.h> using namespace std;int cuts[1000][1000]; int palindrom[1000][1000]; const int INFS = 0x3f3f3f3f; class Solution { public:int minCut(string s) {memset(cuts, 0x3f, sizeof(cuts));memset(palindrom, 0x3f, sizeof(palindrom));int curcuts = countCuts(s,0,s.size()-1);return curcuts;}int countCuts(string &s, int i, int j) {if(j <= i) return 0;if(isPalindrome(s,i,j))return (cuts[i][j]=0);if(cuts[i][j] != INFS)return cuts[i][j];int curcuts = INFS;for(int k = i; k < j; k++) {curcuts = min(curcuts, 1+countCuts(s,i,k)+countCuts(s,k+1,j));}return (cuts[i][j]=curcuts);}bool isPalindrome(string &s, int i, int j) {if(palindrom[i][j] == 1)return true;if(j <= i)return (palindrom[i][j] = true);if(palindrom[i][j] == 0)return false;return (palindrom[i][j] = (s[i]==s[j] && isPalindrome(s,i+1,j-1)));} };int main() {string str = "apjesgpsxoeiokmqmfgvjslcjukbqxpsobyhjpbgdfruqdkeiszrlmtwgfxyfostpqczidfljwfbbrflkgdvtytbgqalguewnhvvmcgxboycffopmtmhtfizxkmeftcucxpobxmelmjtuzigsxnncxpaibgpuijwhankxbplpyejxmrrjgeoevqozwdtgospohznkoyzocjlracchjqnggbfeebmuvbicbvmpuleywrpzwsihivnrwtxcukwplgtobhgxukwrdlszfaiqxwjvrgxnsveedxseeyeykarqnjrtlaliyudpacctzizcftjlunlgnfwcqqxcqikocqffsjyurzwysfjmswvhbrmshjuzsgpwyubtfbnwajuvrfhlccvfwhxfqthkcwhatktymgxostjlztwdxritygbrbibdgkezvzajizxasjnrcjwzdfvdnwwqeyumkamhzoqhnqjfzwzbixclcxqrtniznemxeahfozp";cout << str.size() << endl;cout << (new Solution())->minCut(str) << endl;return 0; }
優(yōu)化后的一維動規(guī)
#include <iostream> #include <memory.h> using namespace std;int cuts[1500]; int palindrom[1500][1500]; const int INFS = 0x3f3f3f3f; class Solution { public:int minCut(string s) {memset(cuts, 0x3f, sizeof(cuts));memset(palindrom, 0x3f, sizeof(palindrom));int curcuts = countCuts(s,0,s.size()-1);return curcuts;}int countCuts(string &s, int i, int j) {if(j <= i) return 0;if(isPalindrome(s,i,j))return 0;if(cuts[i] != INFS)return cuts[i];int curcuts = INFS;for(int k = i; k < j; k++) {if(isPalindrome(s,i,k))curcuts = min(curcuts, 1+countCuts(s,k+1,j));}return (cuts[i]=curcuts);}bool isPalindrome(string &s, int i, int j) {if(palindrom[i][j] == 1)return true;if(j <= i)return (palindrom[i][j] = true);if(palindrom[i][j] == 0)return false;return (palindrom[i][j] = (s[i]==s[j] && isPalindrome(s,i+1,j-1)));} };int main() {string str = "bb";cout << str.size() << endl;cout << (new Solution())->minCut(str) << endl;return 0; }
I
第一題用動態(tài)規(guī)劃也是可以做的, 不過會比較麻煩(與Word Break類似)
這里用 dfs 加打印路徑, 比較直觀
int palindrom[1500][1500]; vector<vector<string> > res; class Solution { public:vector<vector<string>> partition(string s) {res.clear();memset(palindrom, 0x3f, sizeof(palindrom));vector<string> tmp;dfs(s, tmp, 0);return res;}bool isPalindrome(string &s, int i, int j) {if(palindrom[i][j] == 1)return true;if(j <= i)return (palindrom[i][j] = true);if(palindrom[i][j] == 0)return false;return (palindrom[i][j] = (s[i]==s[j] && isPalindrome(s,i+1,j-1)));}void dfs(string &s, vector<string> cur_vec, int depth) {if(depth == s.size()) {res.push_back(cur_vec);return;}for(int i = depth; i < s.size(); i ++) {if(isPalindrome(s, depth,i)) {cur_vec.push_back(s.substr(depth,i-depth+1));dfs(s, cur_vec, i+1);cur_vec.pop_back();}}} };
總結(jié)
以上是生活随笔為你收集整理的Leetcode: Palindrome Partition I II的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【机器视觉学习笔记】大津法/Otsu最大
- 下一篇: 【Unity3D日常开发】Unity3D