Palindrome Partitioning
来源:互联网 发布:linux 驱动与 win驱动 编辑:程序博客网 时间:2024/05/21 14:03
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
java
public class Solution { /* * @param s: A string * @return: A list of lists of string */ public List<List<String>> partition(String s) { // write your code here List<List<String>> result = new ArrayList<>(); if (s == null || s.length() == 0) { return result; } List<String> list = new ArrayList<>(); if (s.length() == 1) { list.add(s); result.add(list); return result; } dfs(s, 0, list, result); return result; } private void dfs(String s, int startIndex, List<String> list, List<List<String>> result) { if (startIndex == s.length()) { result.add(new ArrayList<String>(list)); } for (int i = startIndex; i < s.length(); i++) { String sub = s.substring(startIndex, i + 1); if (!isPalindrome(sub)) { continue; } else { list.add(sub); dfs(s, i + 1, list, result); list.remove(list.size() - 1); } } } private boolean isPalindrome(String s) { if (s == null || s.length() == 0) { return false; } char[] c = s.toCharArray(); int i = 0; int j = c.length - 1; while (i <= j) { if (c[i++] != c[j--]) { return false; } } return true; }}
阅读全文
0 0
- Palindrome partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- Palindrome Partitioning
- 多线程面试题
- LeetCode- Find Bottom Left Tree Value
- Java进程突然消失
- eclipse 常用快捷键(一)
- Python学习:连接MySQL数据库
- Palindrome Partitioning
- 计算机科学 —— 时间戳(timestamp)
- nginx与apache优缺点比较
- P1062 数列
- 欢迎使用CSDN-markdown编辑器
- 图解ARP协议(四)代理ARP:善意的欺骗
- Java 有两个任务A与B,多线程执行任务A完成后再继续执行任务B
- CS224D:Deep Learning for NLP Note1
- Java序列化与反序列化