回文子串划分 Palindrome Partitioning
来源:互联网 发布:mac上的打谱软件 编辑:程序博客网 时间:2024/06/05 14:37
问题:将一个字符串进行划分,要求划分成的子串都必须是回文串。找出所有可能的划分情况。
思路:backstrack吧,每一次只有在满足是回文的情况下深入下一层。
class Solution {public: vector<vector<string>> partition(string s) { vector<vector<string> > result; vector<string> now; if(s.size() == 0) return result; backtrack(s, 0, now, result); return result; } void backtrack(string s, int idx, vector<string> &now, vector<vector<string> > &result) { int n = s.size(); if(idx == n) { result.push_back(now); return; } for(int i=idx;i<n;i++) { if(isPalindrome(s, idx, i)) { now.push_back(s.substr(idx, i-idx+1)); backtrack(s, i+1, now, result); now.pop_back(); } } } bool isPalindrome(string s, int left, int right) { while(left <= right) { if(s[left++] != s[right--]) return false; } return true; }};
0 0
- 回文子串划分 Palindrome Partitioning
- leetcode【Palindrome Partitioning 回文字符串划分】
- 【LeetCode】131.Palindrome Partitioning回文划分
- leetcode 131. Palindrome Partitioning回文子串+DFS
- Palindrome Partitioning 分割成回文子字符串
- Light Oj 1044 - Palindrome Partitioning(回文串)
- [Lintcode]Palindrome Partitioning分割回文串
- LeetCode------------palindrome-partitioning(回文)
- LeetCode | Palindrome Partitioning(将一个子串划分成回文子串)
- LeetCode | Palindrome Partitioning II(最少切割次数将一个子串划分成回文子串)
- [C++]LeetCode: 121 Palindrome Partitioning (分割回文子串 回溯法)
- Light oj 1044 - Palindrome Partitioning (多校连萌,求最少回文子串的个数)
- leetcode 132. Palindrome Partitioning II 回文子串 + DFS + DP + 这道题需要认真学习
- 判断字符串string所有分割和子串是否为回文(palindrome)并返回结果(Palindrome Partitioning)
- [LeetCode] Palindrome Partitioning 回文分割
- Palindrome Partitioning&II(回文字符串)
- Palindrome Partitioning 回文串分割,求所有可能的分割
- 136.Palindrome Partitioning-分割回文串(中等题)
- 面向对象
- 容斥原理(SOJ3082)
- PL/SQL_游标使用
- R软件初识数据分析
- IOS学习中的几个问题
- 回文子串划分 Palindrome Partitioning
- C++实现单链表的逆转
- 用SeekBar更改图片的大小,缩放及其旋转
- 笔试面试杂记
- Leetcode_climbing-statirs (c++ and python version)
- 认识NPOI
- NAT and Traversal NAT(TURN/STUN/ICE)
- 标准CSV格式
- 继续说下我知道的Controllers的知识哦