131. Palindrome Partitioning

    xiaoxiao2021-03-25  109

    public class Solution { public void help(boolean f[][],String s,List<List<String>> result, List<String> path, int pos) { if(s.length() == pos) { result.add(new ArrayList<>(path)); return; } for(int j = pos; j < s.length();j++) if(f[pos][j]) { path.add(s.substring(pos,j+1)); help(f,s,result,path,j+1); path.remove(path.size()-1); } } public List<List<String>> partition(String s) { boolean[][] f = new boolean[s.length()][s.length()]; for(int i = 0; i < s.length(); i++) { for(int j = 0; j <= i; j++) { if(s.charAt(i) == s.charAt(j) && (i-j<=2 || f[j+1][i-1])) { f[j][i] = true; } } } List<List<String>> result = new ArrayList<>(); List<String> path = new ArrayList<>(); help(f,s,result,path,0); return result; } }
    转载请注明原文地址: https://ju.6miu.com/read-19130.html

    最新回复(0)