[leetcode]131 Palindrome Partitioning
来源:互联网 发布:sql数据库课程设计题目 编辑:程序博客网 时间:2024/04/29 23:39
问题描述:
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 boolean isPalindrome(String s) { int len = s.length(); if(len == 0) return true; int mid = (len-1)/2; for(int i =0; i <=mid; i++) if(s.charAt(i) != s.charAt(len-1-i)) return false; return true; } public void getPartation(List<List<String>> result, List<String>list, List<List<Integer>> part,String s,int begin){ if(list == null) list = new ArrayList<String>(); if(begin == s.length()){ result.add(list); return; } for(int i = 0; i < part.get(begin).size(); i++) { List<String> temp = new ArrayList<String>(list); int len = part.get(begin).get(i); temp.add(s.substring(begin,begin+len)); getPartation(result,temp,part,s,begin+len); } } public List<List<String>> partition(String s) { int len = s.length(); List<List<String>> result = new ArrayList<List<String>>(); if(len == 0) return result; List<List<Integer>> part = new ArrayList<List<Integer>>(); for(int i = 0; i<len; i++){ List<Integer> list = new ArrayList<Integer>(); for(int j = i+1; j <=len; j++) { String temp = s.substring(i,j); if(isPalindrome(temp)) list.add(j-i); } part.add(list); } List<String> list = null; getPartation(result,list,part,s,0); return result; }
0 0
- LeetCode: Palindrome Partitioning [131]
- 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
- 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
- php实现http登陆验证
- error C2664
- 设置Tomcat的UTF-8编码
- error LNK2019
- Dronecode Project:无人机的开源平台
- [leetcode]131 Palindrome Partitioning
- 解析字符串 java
- strut2 iterator标签的使用
- MVC下载Excel文件:js接受乱码 简单处理办法
- ace bootstrap后台框架(一)
- 凸包扫描 + 旋转卡壳 UVALive 4728
- web项目修改根目录的方法
- struts2 的struts.properties(xml)配置详解
- uva572 - Oil Deposits 入门经典II 第六章数据结构基础 例题6-12