LeetCode 131. Palindrome Partitioning
来源:互联网 发布:c语言简单小游戏 编辑:程序博客网 时间:2024/06/06 05:16
直接使用DFS硬怼
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"]]
java
class Solution { public List<List<String>> partition(String s) { List<List<String>> result = new ArrayList<>(); if (s == null || s.length() == 0) { return result; } dfs(s, 0, new ArrayList<String>(), result); return result; } private void dfs(String s, int start, List<String> path, List<List<String>> result) { if (start == s.length()) { result.add(new ArrayList<String>(path)); return; } for (int i = start; i < s.length(); i++) { String str = s.substring(start, i + 1); if (!isPal(str)) { continue; } else { path.add(str); dfs(s, i + 1, path, result); path.remove(path.size() - 1); } } } private boolean isPal(String s) { if (s.length() == 1) { return true; } int left = 0; int right = s.length() - 1; while (left <= right) { if (s.charAt(left++) != s.charAt(right--)) { return false; } } return true; }}
阅读全文
0 0
- LeetCode 131. Palindrome Partitioning
- [LeetCode]131.Palindrome Partitioning
- [Leetcode] 131. Palindrome Partitioning
- [leetcode] 131.Palindrome Partitioning
- [LeetCode]131. Palindrome Partitioning
- 131. Palindrome Partitioning LeetCode
- Leetcode 131. Palindrome Partitioning
- LeetCode 131. Palindrome Partitioning
- LeetCode *** 131. Palindrome Partitioning
- LeetCode 131. Palindrome Partitioning
- LeetCode-131.Palindrome Partitioning
- 【leetcode】131. Palindrome Partitioning
- [leetcode] 131. Palindrome Partitioning
- 【Leetcode】131. Palindrome Partitioning
- [LeetCode] 131. Palindrome Partitioning
- Leetcode-131. Palindrome Partitioning
- 【LeetCode】131. Palindrome Partitioning
- Leetcode 131. Palindrome Partitioning
- Nginx配置文件详细说明
- csdn博客改版
- 用GDB调试程序(三)
- Asp.net Web Api开发(第一篇) 自定义HTTP消息拦截器
- 线性表笔记
- LeetCode 131. Palindrome Partitioning
- 在web上面显示地图并定位
- 关于Python中re模块的详细使用说明
- Python编程入门学习笔记——列表
- spring aop实现权限控制
- 代理模式
- 大小写转换
- 结合JsBarcode文档整理出来的一个demo,完全够用,生成和打印条形码
- WCF中的Binding模型之二: 信道与信道栈(Channel and Channel Stack)