LeetCode 131. Palindrome Partitioning
来源:互联网 发布:严格模式js有什么好处 编辑:程序博客网 时间:2024/05/29 18:40
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”]
]
解题方法,首先求解出所有的子字符串,然后使用双指针的方式进行判断是不是回文串。
public class Solution { public List<List<String>> partition(String s) { List<List<String>> result = new ArrayList<>(); if (s == null || s.length() == 0) { return result; } List<String> arr = new ArrayList<>(); util(result, arr, s,0); return result; } private void util(List<List<String>> result, List<String> arr, String s, int start){ if (start == s.length()) { result.add(new ArrayList<>(arr)); return; } for (int i = start; i < s.length(); i++) { String substr = s.substring(start, i + 1); if (isPalindrome(substr)) { arr.add(substr); util(result, arr, s, i + 1); arr.remove(arr.size() - 1); } } } private boolean isPalindrome(String str) { if (str.length() == 0) { return true; } int left = 0; int right = str.length() - 1; while (left <= right) { if(str.charAt(left++) != str.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
- Linux——touch命令
- 数据结构实验之链表七:单链表中重复元素的删除
- 对于中文的Cookie
- Android Binder系统的理解。
- Codeforces Round #398 (Div. 2) A.Snacktower B.The Queue C.Garland D.Cartons of milk E.Change-fr
- LeetCode 131. Palindrome Partitioning
- sql语句优化大全
- 欢迎使用CSDN-markdown编辑器
- [leetcode]504. Base 7
- node.js not linked error
- win 7 远程桌面连接
- bzoj2049 [Sdoi2008]Cave 洞穴勘测
- 有序矩阵查找练习题
- 为heat安装docker插件