Palindrome Partitioning
2015-08-10 16:00
197 查看
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
思路:采用组合的思想,从下标idx的字符开始,选择第一个为回文串的子字符串,选择该子回文字符串后,问题将转化为求除去该子字符串之后剩余字符串的子回文串。可以发现用递归的方式,判断字符串是否为回文串,这个函数isPalindrome会不断的重复计算,耗时比较长,可以把递归的方式过程中判断字符串是否为回文字符串的结果记录在一个二维表里面,将省去重复的计算。
递归的方式:
迭代的方式:
如果isPalindrome[i][j]=1,那么从i到j的子字符串是回文串,否则如果isPalindrome[i][j]=0,从i到j的子字符串不是回文串。
对于isPalindrome[i][j]的计算:
如果i==j ,那么isPalindrome[i][j]=1;
否则,如果s.charAt(i)==s.chatAt(j)
此时如果j=i+1,那么isPalindrome[i][j]=1;
此时如果j!=i+1,那么从i到j为回文串的必要条件是isPalindrome[i+1][j-1]=1
注意:在updateArray(s,isPalindrome);这个函数中,更新isPalindrome[i][j]的时候,要保证isPalindrome[i+1][j-1]已经更新完了,所以可以从最后一行开始更新,那么在更新上一行的时候,下一行已经更新完了。
Return all possible palindrome partitioning of s.
For example, given s = “aab”,
Return
[ ["aa","b"], ["a","a","b"] ]
思路:采用组合的思想,从下标idx的字符开始,选择第一个为回文串的子字符串,选择该子回文字符串后,问题将转化为求除去该子字符串之后剩余字符串的子回文串。可以发现用递归的方式,判断字符串是否为回文串,这个函数isPalindrome会不断的重复计算,耗时比较长,可以把递归的方式过程中判断字符串是否为回文字符串的结果记录在一个二维表里面,将省去重复的计算。
递归的方式:
public class Solution { public List<List<String>> partition(String s) { List<List<String>> res=new ArrayList<List<String>>(); if(s==null || s.length()==0 || s.equals("")) return res; List<String> temp=new ArrayList<String>(); partition(res,temp,0,s); return res; } public void partition(List<List<String>> res,List<String> temp,int idx,String s) { if(idx==s.length()) { List<String> new_temp=new ArrayList<String>(temp); res.add(new_temp); return; } for(int i=idx+1;i<=s.length();i++) { if(isPalindrome(s.substring(idx, i))) { temp.add(s.substring(idx,i)); partition(res,temp,i,s); temp.remove(temp.size()-1); } } } private boolean isPalindrome(String substring) { int i=0; int j=substring.length()-1; int mid=i+(j-i)/2; while(i<=mid) { if(substring.charAt(i)!=substring.charAt(j)) return false; else { i++; j--; } } return true; } }
迭代的方式:
如果isPalindrome[i][j]=1,那么从i到j的子字符串是回文串,否则如果isPalindrome[i][j]=0,从i到j的子字符串不是回文串。
对于isPalindrome[i][j]的计算:
如果i==j ,那么isPalindrome[i][j]=1;
否则,如果s.charAt(i)==s.chatAt(j)
此时如果j=i+1,那么isPalindrome[i][j]=1;
此时如果j!=i+1,那么从i到j为回文串的必要条件是isPalindrome[i+1][j-1]=1
注意:在updateArray(s,isPalindrome);这个函数中,更新isPalindrome[i][j]的时候,要保证isPalindrome[i+1][j-1]已经更新完了,所以可以从最后一行开始更新,那么在更新上一行的时候,下一行已经更新完了。
public class Solution { public List<List<String>> partition(String s) { List<List<String>> res=new ArrayList<List<String>>(); int n=s.length(); if(s==null || n==0 || s.equals("")) return res; int[][] isPalindrome=new int ; updateArray(s,isPalindrome); List<String> temp=new ArrayList<String>(); partition(res,temp,0,s,isPalindrome); return res; } public void updateArray(String s,int[][] isPalindrome) { int n=s.length(); for(int i=n-1;i>=0;i--) { for(int j=i;j<n;j++) { if(i==j) isPalindrome[i][j]=1; else if(s.charAt(i)==s.charAt(j)) { if((j==i+1) || (isPalindrome[i+1][j-1]==1)) isPalindrome[i][j]=1; } } } } public void partition(List<List<String>> res,List<String> temp,int idx,String s,int[][] isPalindrome) { if(idx==s.length()) { List<String> new_temp=new ArrayList<String>(temp); res.add(new_temp); return; } for(int i=idx+1;i<=s.length();i++) { if(isPalindrome[idx][i-1]==1) { temp.add(s.substring(idx,i)); partition(res,temp,i,s,isPalindrome); temp.remove(temp.size()-1); } } } }
相关文章推荐
- SOLR问答
- Zend_Form 创建、校验和解析表单的基础--(手册)
- HDU 5060/BC 12C War
- Android中设置TextView的颜色setTextColor
- Android中Services简析
- springmvc学习第二天
- hibernate.hbm2ddl.auto配置详解
- 后台弹出JS类
- Sequence contains no matching element
- Repeater
- Android中设置TextView的颜色setTextColor
- 通过VFL语法添加约束
- 深度学习(DL)与卷积神经网络(CNN)学习笔记随笔-01-CNN基础知识点
- Linux - crontab的创建以及注意事项
- CF Clique in the Divisibility Graph (DP)
- 欢迎使用CSDN-markdown编辑器
- SDUT 1500 Message Flood (快排+二分查找)
- Linux - crontab的创建以及注意事项
- 被碎片化的时间
- svn使用教程