首页 > 代码库 > [leetcode]131 Palindrome Partitioning
[leetcode]131 Palindrome Partitioning
问题描述:
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; }
[leetcode]131 Palindrome Partitioning
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。