题目一, 题目二

思路

1. 第一遍做时就参考别人的, 现在又忘记了 做的时候使用的是二维动态规划, 超时加超内存

2. 只当 string 左部分是回文的时候才有可能减少 cut

3. 一维动规. 令 cuts[i] 表示string[i, string.size()] 所需的切割数, 那么

状态转移方程为 cuts[i] = min(cuts[j]+1) j > i && string[i, j] is palindrome

时间复杂度上仍是 o(n*n), 但更新 cuts 的限制条件比较多了, cuts[i] 更新频率较低

代码:

超时二维动规代码

#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;
}

  

优化后的一维动规

#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

第一题用动态规划也是可以做的, 不过会比较麻烦(与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();
}
}
}
};

  

Leetcode: Palindrome Partition I II的更多相关文章

  1. LeetCode: Palindrome Partition

    LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...

  2. [Leetcode] palindrome partition ii 回文分区

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  3. [LeetCode] Palindrome Permutation I & II

    Palindrome Permutation Given a string, determine if a permutation of the string could form a palindr ...

  4. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  5. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  6. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  7. Palindrome Partitioning I & II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  8. LeetCode: Palindrome 回文相关题目

    LeetCode: Palindrome 回文相关题目汇总 LeetCode: Palindrome Partitioning 解题报告 LeetCode: Palindrome Partitioni ...

  9. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

随机推荐

  1. eval函数处理JSON数据需要加括号

    在将服务器端构建好的JSON数据转化为可用的JavaScript对象时常常使用eval函数.如下: var dataJson = eval('(' + data + ')'); 在转化的时候需要将JS ...

  2. iOS开发-多线程开发之线程安全篇

    前言:一块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源,比如多个线程访问同一个对象.同一个变量.同一个文件和同一个方法等.因此当多个线程访问同一块资源时,很容易会发生数据错误及数据不安 ...

  3. Sphinx全文检索引擎测试

    数据表 1.documents CREATE TABLE `documents` ( `id` int(13) NOT NULL auto_increment, `group_id` int(11) ...

  4. CRC文件解压缩问题

    CRC问题一般有三种可能1.你的硬盘出现坏道2.你的硬盘数据线受损3.还可能是主板和内存的问题 硬盘坏道的表现硬盘使用久了就可能出现各种各样的问题,而硬盘“坏道”便是这其中最常见的问题.硬盘出现坏道除 ...

  5. 机器学习基石第三讲:types of learning

    博客已经迁移至Marcovaldo's blog (http://marcovaldong.github.io/) 刚刚完毕机器学习基石的第三讲.这一讲主要介绍了机器学习的分类.对何种问题应该使用何种 ...

  6. mysql误删root用户或者忘记root密码解决方法

    解决方法一: 到其他安装了Mysql的服务器(前提是要知道该服务器上Mysql的root用户密 码),打开[Mysql的安装目录/var/mysql],将其中的user.frm.user.MYD.us ...

  7. 浏览器 本地预览图片 window.url.createobjecturl

    第一种方式 <script type="text/javascript"> function setImagePreview() { var docObj = docu ...

  8. 基于HTML5坦克大战游戏简化版

    之前我们有分享过不少经典的HTML5游戏,有些还是很有意思的,比如HTML5版切水果游戏和HTML5中国象棋游戏.今天要分享的是一款简化版的HTML5坦克大战游戏,方向键控制坦克的行进方向,空格键发射 ...

  9. Mybatis Batch 批量操作

    Mybatis Batch 批量操作 http://www.blogjava.net/diggbag/articles/mybatis.html

  10. API - 使用Default对象 - 基础篇

    在编写Spider Studio脚本时, Default对象是最常用最重要的一个, 其类型定义如下: Webus3.Spider.Controls.JQueryBrowser Default; 下面介 ...