题目一, 题目二

思路

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. cocos2d-x 源代码分析 : EventDispatcher、EventListener、Event 源代码分析 (新触摸机制,新的NotificationCenter机制)

    源代码版本号来自3.x,转载请注明 cocos2d-x 源代码分析总文件夹 http://blog.csdn.net/u011225840/article/details/31743129 1.继承结 ...

  2. C# 通过form表单下载文本文件

    public void DownLoadConfigFile(string name) { //获取文件字符串内容 var data = _service.ReadFileStr(_configure ...

  3. Linux命令-文件搜索命令:find

    选项: -name表示按文件名称查找 find /etc -name init 搜索etc目录下面的文件名为init的所有文件(精确搜索) find /etc -name *init* 搜索etc目录 ...

  4. 嵌入式Linux下ALSA音频架构ALSA-lib移植与编译心得

    **************************************************************************************************** ...

  5. HTML中css和js链接中的版本号

    背景 在搜索引擎中搜索关键字.htaccess 缓存,你可以搜索到很多关于设置网站文件缓存的教程,通过设置可以将css.js等不太经常更新的文件缓存在浏览器端,这样访客每次访问你的网站的时候, 浏览器 ...

  6. Jquery 大纲

    1,核心 核心函数 对象訪问 数据缓存 队列控制 插件机制 多库共存 2.选择器 基本选择器 层级 基本 内容 可见性 属性 子元素 表单 表单对象属性 3.文档处理 内部插入 外部插入 包裹 替换 ...

  7. 第二节 JVM优化应用以及知识总结

    在JVM中.假设98%的时间是用于GC且可用的HeapSize不足2%时将会抛出OOM异常:HeapSize最大不要超过可用物理内存的80%,一般-Xms –Xmx设置为同样,-Xmn设置为1/4的- ...

  8. ubuntu下安装自动补全YouCompleteMe

    一.安装预备软件.#vim要带python2.7的支持,curl是下载插件必须用到的软件,还有git apt install vim-nox-py2 curl git #安装python头文件 apt ...

  9. 实现编程时Vim自动导入相应模板

    Vim文本编辑器以简洁高效著称,那么我们在编程时能有自动加载相应的模板,从而省去一些固定的输入提升工作效率呢!当然可以,可以有多种方法实现,我这里介绍一种非常简单的方法. 首先在你的主用户文件下面建立 ...

  10. Struts2初学 struts2自定义类型转换器

    一.问题的引出      Struts2的类型转换是基于OGNL表达式的,由于请求的参数都是字符串,而JAVA 本身属于强类型的的语言,这样就需要把请求参数字符串转换成其他类型.     Struts ...