LeetCode 题解(152): Palindrome Partitioning
来源:互联网 发布:爱奇艺出品的网络剧 编辑:程序博客网 时间:2024/05/19 19:41
题目:
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"] ]
题解:
递归, Backtracing。仅当当前子串为Palindrome时,递归后半。
C++版:
class Solution {public: vector<vector<string>> partition(string s) { vector<vector<string>> result; vector<string> output; recursion(s, 0, output, result); return result; } void recursion(string s, int start, vector<string>& output, vector<vector<string>>& result) { if(start == s.length()) { result.push_back(output); return; } for(int i = start; i < s.length(); i++) { if(isPalindrome(s, start, i)) { output.push_back(s.substr(start, i - start + 1)); recursion(s, i + 1, output, result); output.pop_back(); } } } bool isPalindrome(string s, int start, int end) { while(start < end) { if(s[start++] != s[end--]) return false; } return true; }};
Java版:
public class Solution { public List<List<String>> partition(String s) { List<List<String>> result = new ArrayList<List<String>>(); List<String> output = new ArrayList<String>(); recursion(s, 0, output, result); return result; } void recursion(String s, int start, List<String> output, List<List<String>> result) { if(start == s.length()) { ArrayList<String> temp = new ArrayList<>(); temp.addAll(output); result.add(temp); return; } for(int i = start; i < s.length(); i++) { if(isPalindrome(s, start, i)) { output.add(s.substring(start, i+1)); recursion(s, i + 1, output, result); output.remove(output.size()-1); } } } public boolean isPalindrome(String s, int start, int end) { while(start < end) { if(s.charAt(start++) != s.charAt(end--)) return false; } return true; }}
Python版:
class Solution: # @param {string} s # @return {string[][]} def partition(self, s): result = [] output = [] self.recursion(s, 0, output, result) return result def recursion(self, s, start, output, result): if start == len(s): result.append(copy.copy(output)) for i in range(start, len(s)): if self.is_palindrome(s, start, i): output.append(s[start:i+1]) self.recursion(s, i + 1, output, result) output.pop() def is_palindrome(self, s, start, end): while start < end: if s[start] != s[end]: return False start += 1 end -= 1 return True
0 0
- LeetCode 题解(152): Palindrome Partitioning
- LeetCode题解:Palindrome Partitioning
- LeetCode 题解(90): Palindrome Partitioning II
- Palindrome Partitioning II|leetcode题解
- LeetCode - Palindrome Partitioning II 题解
- 【leetcode题解】【M】【79】Palindrome Partitioning
- LeetCode题解–131. Palindrome Partitioning
- LeetCode(131)Palindrome Partitioning
- 【leetcode】Palindrome Partitioning && Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning && Palindrome Partitioning II
- LeetCode Palindrome Partitioning I&&Palindrome Partitioning II
- LeetCode Palindrome Partitioning, Palindrome Partitioning II
- [Leetcode][python]Palindrome Partitioning/Palindrome Partitioning II
- Leetcode:Palindrome Partitioning & Palindrome Partitioning II
- LeetCode 131. Palindrome Partitioning(回文分区)
- Leetcode 132 - Palindrome Partitioning II(dp)
- LeetCode: Palindrome Partitioning
- LeetCode : Palindrome Partitioning II
- ZZUACM 2015 暑假集训 round 02
- Java多线程
- CSS 最核心的几个概念
- Redis命令——Keys相关
- javascript 将arguments 转化为array [].slice.apply(arguments)
- LeetCode 题解(152): Palindrome Partitioning
- POJ 2926 Requirements (多维最远曼哈顿距离)
- div 居中(html5+jquery.mobile)
- PHP empty()
- Java IO流--字节流、File类
- java方法区
- jfinal 文件上传下载
- FFmpeg滤镜文档-使用指南
- bootstrap 判断设备大小改变网格布局