【称号】

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

Return all possible palindrome partitioning of s.

For example, given s = "aab",

Return

  [
["aa","b"],
["a","a","b"]
]

【题意】

给定一个字符串s, 要求对s进行划分,划分后每一个子串都是回文串。

要求返回全部的划分情况

【思路】

直观思路是使用逐层递归。先确定第一个点。然后确定第二个点,再确定第三个点,依次类推。这样的方式的时间复杂度很高。

    

    本题使用DP:先计算出随意两个位置i,j之间的子串是否是回文串,用IsPalindrome[i][j]表示。

【代码】

class Solution {
public: void getPartition(vector<vector<string> >&result, vector<string>&splits, int start, string&s, vector<vector<bool> >&isPal){
//spits-已切分的结果,start-当前切分開始的位置
if(start==s.length()){
vector<string> newSplits = splits;
result.push_back(newSplits);
return;
} for(int end=start; end<s.length(); end++){
if(isPal[start][end]){
splits.push_back(s.substr(start, end-start+1));
getPartition(result, splits, end+1, s, isPal);
splits.pop_back();
}
}
} vector<vector<string>> partition(string s) {
vector<vector<string> >result;
int len=s.length();
if(len==0)return result; vector<vector<bool> > isPal(len, vector<bool>(len, false));
//初始化isPal[i][i]=true;
for(int i=0; i<len; i++)
isPal[i][i]=true;
//初始化相邻两位字符构成的子串
for(int i=0; i<len-1; i++)
if(s[i]==s[i+1])isPal[i][i+1]=true;
//推断其它i<j的位置
for(int i=len-3; i>=0; i--)
for(int j=i+2; j<len; j++)
isPal[i][j]=(s[i]==s[j]) && isPal[i+1][j-1]; //确定全部的组合
vector<string> splits;
getPartition(result, splits, 0, s, isPal);
return result;
}
};

版权声明:本文博主原创文章。博客,未经同意不得转载。

LeetCode: Palindrome Partitioning [131]的更多相关文章

  1. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

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

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

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

  3. [LeetCode] Palindrome Partitioning II 拆分回文串之二

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

  4. [LeetCode] Palindrome Partitioning 拆分回文串

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

  5. [leetcode]Palindrome Partitioning II @ Python

    原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s  ...

  6. [leetcode]Palindrome Partitioning @ Python

    原题地址:https://oj.leetcode.com/problems/palindrome-partitioning/ 题意: Given a string s, partition s suc ...

  7. LeetCode: Palindrome Partitioning 解题报告

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

  8. Leetcode: Palindrome Partitioning II

    参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...

  9. [Leetcode] Palindrome Partitioning

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

随机推荐

  1. c++对象指针-01(转载)

    1.指向对像的指针在建立对像时,编译系统会为每一个对像分配一定的存储空间,以存放其成员,对像空间的起始地址就是对像的指针.可以定义一个指针变量,用来存和对像的指针.如果有一个类:class Time{ ...

  2. 数字证书及CA的扫盲介绍(转)

    ★ 先说一个通俗的例子 考虑到证书体系的相关知识比较枯燥.晦涩.俺先拿一个通俗的例子来说事儿. ◇ 普通的介绍信 想必大伙儿都听说过介绍信的例子吧?假设 A 公司的张三先生要到 B 公司去拜访,但是 ...

  3. HDU1071 The area 【积分】

    The area Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  4. Ansible@一个高效的配置管理工具--Ansible configure management--翻译(八)

    如无书面授权,请勿转载 第四章,大型项目中Ansible的使用 Roles If your playbooks start expanding beyond what includes can hel ...

  5. Callable 获取线程返回值

    allable与 Future 两功能是Java在兴许版本号中为了适应多并法才增加的,Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其它 ...

  6. Cstyle的UEFI导读:第20.0篇 IGD OpRegion interface &amp;&amp; IGD OpRegion PROTOCOL

        ACPI IGD OpRegion interface是用SCI来实现IGD driver,OS,BIOS之间沟通的桥梁,IGD OpRegion PROTOCOL是UEFI BIOS构建桥梁 ...

  7. WebPack实例与前端性能优化

    [前端构建]WebPack实例与前端性能优化   计划把微信的文章也搬一份上来. 这篇主要介绍一下我在玩Webpack过程中的心得.通过实例介绍WebPack的安装,插件使用及加载策略.感受构建工具给 ...

  8. js日期天数差

    var s1 = "2007-01-01"; var s2 = "2007-12-31"; s1 = s1.replace(/-/g, "/" ...

  9. [Windows Phone] 地图控制项的经纬度

    原文:[Windows Phone] 地图控制项的经纬度 前言 本文主要示范如何使用地图经纬度以及显示地标和行人街道,并透过卷轴控制地图缩放比例的功能. ? 实作 step1 建立专案. ? step ...

  10. 【Android笔记】MediaPlayer基本用法

    Android MediaPlayer基本使用方式 使用MediaPlayer播放音频或者视频的最简单样例: JAVA代码部分: public class MediaPlayerStudy exten ...