Palindrome Partitioning 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/palindrome-partitioning/description/


Description

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"]
]

Solution

class Solution {
public:
vector<vector<string>> partition(string s) {
int len = s.length();
vector<vector<string>> res;
vector<string> path;
dfs(0, s, path, res);
return res;
} void dfs(int idx, string& str, vector<string>& path, vector<vector<string>>& res) {
if (idx == str.length()) {
res.push_back(path);
return;
}
for (int i = idx; i < str.size(); i++) {
if (isPalindrome(str, idx, i)) {
path.push_back(str.substr(idx, i - idx + 1));
dfs(i + 1, str, path, res); /* pop back every time in recurse stack,
* than all the paces added in dfs can be remove */
path.pop_back();
}
}
} bool isPalindrome(string& str, int start, int end) {
while (start < end) {
if (str[start] != str[end]) {
return false;
}
start++;
end--;
}
return true;
}
};

解题描述

这道题是目的是找到一个字符串中所有由回文子串组成的集合,算法是对给出的字符串进行遍历,查找所有回文子串,对每个回文子串再进行DFS查找新的回文子串,这样就能找到所有由回文子串切分的子串的集合。

[Leetcode Week13]Palindrome Partitioning的更多相关文章

  1. [LeetCode] 131. Palindrome Partitioning 回文分割

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

  2. Leetcode 131. Palindrome Partitioning

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

  3. 【leetcode】Palindrome Partitioning II(hard) ☆

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

  4. [Leetcode][JAVA] Palindrome Partitioning II

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

  5. 【leetcode】Palindrome Partitioning II

    Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...

  6. 【leetcode】Palindrome Partitioning

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

  7. leetcode 132. Palindrome Partitioning II ----- java

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

  8. leetcode之 Palindrome Partitioning I&II

    1 Palindrome Partitioning 问题来源:Palindrome Partitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有 ...

  9. [LeetCode] 132. Palindrome Partitioning II_ Hard tag: Dynamic Programming

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

随机推荐

  1. Java取两个变量不为空的变量的简便方法!

    一.需求 最近在项目中遇到一个小问题,即从数据库取两个变量,判断取出的变量是否为空,取不为空的变量:若两个变量都不为空,取两个变量:两个变量都为空,则跳过: 二.解决方案(这里提供两种思路) 1.第一 ...

  2. Kubernetes初探 :总体概述及使用示例

    Kubernetes是Google开源的容器集群管理系统.它构建于docker技术之上,为容器化的应用提供资源调度.部署运行.服务发现.扩容缩容等整一套功能,本质上可看作是基于容器技术的mini-Pa ...

  3. cogs1667[SGU422]傻叉小明打字

    其实和CF498bName that Tune差不多 题意: 现在需要依次输入n个字符,第i个字符输入的时候有pi的概率输错,不论是第几次输入(0<=pi<=0.5).每输入一个字符的用时 ...

  4. BZOJ5466 NOIP2018保卫王国(倍增+树形dp)

    暴力dp非常显然,设f[i][0/1]表示i号点不选/选时i子树内的答案,则f[i][0]=Σf[son][1],f[i][1]=a[i]+Σmin(f[son][0],f[son][1]). 注意到 ...

  5. Socket网络编程实例2

    两个程序通过“网络”交互数据就使用socket,它只负责两件事:建立连接,传递数据. 所有的数据传输接收,必须都使用byte格式 1.简单实例: #客户端 import socket client=s ...

  6. BZOJ1486:[HNOI2009]最小圈——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=1486 https://www.luogu.org/problemnew/show/P3199 题面 ...

  7. BZOJ4870:[SHOI2017]组合数问题——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=4870 https://www.luogu.org/problemnew/show/P3746 看网上 ...

  8. Jsp电子商务之七 订单篇2

    从View页面,点击超链接查询订单,进入到控制器 OrderlistServlet package com.cart.web; import java.io.IOException; import j ...

  9. Ubuntu 16.04安装NVIDIA驱动后循环登录问题

    问题描述 最近买了两块NVIDIA Titan X Pascal显卡装到了服务器(运行Ubuntu 16.04)上.为了使用这两块GPU显卡,首先需要安装显卡驱动,安装方式为 #安装一个依赖文件,并更 ...

  10. STL使用总结

    转载于http://blog.csdn.net/daisy_chenting/article/details/6898184 1.    概述 泛型编程思想最早缘于A.Stepanov提出的部分算法可 ...