zoukankan      html  css  js  c++  java
  • 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.

    Example:

    Input: "aab"
    Output:
    [
      ["aa","b"],
      ["a","a","b"]
    ]

    思路问题:自己的思路没问题

    结束条件:表示pos到头
    if(pos==s.length()) res.add(new ArrayList<String>(list));
    
    

    i的for循环里,应该是i + 1



    public class Solution {
        public List<List<String>> partition(String s) {
           List<List<String>> res = new ArrayList<List<String>>();
           List<String> list = new ArrayList<String>();
           dfs(s,0,list,res);
           return res;
        }
        
        public void dfs(String s, int pos, List<String> list, List<List<String>> res){
            if(pos==s.length()) res.add(new ArrayList<String>(list));//表示pos到头了
            else{
                for(int i=pos;i<s.length();i++){
                    if(isPal(s,pos,i)){
                        list.add(s.substring(pos,i+1));
                        dfs(s,i+1,list,res); //i的for循环里,应该是i + 1
                        list.remove(list.size()-1);
                    }
                }
            }
        }
        
        public boolean isPal(String s, int low, int high){
            while(low<high) if(s.charAt(low++)!=s.charAt(high--)) return false;
            return true;
        }
        
    }
    View Code
     
  • 相关阅读:
    linux驱动模板
    Matlab的安装
    级数模型
    多元思维
    一级指针和二级指针
    Biology 053: Visualization of Digestive Enzymes
    Biology 052: Digestion Experiment
    Biology 06: Metabolism and Nutrition
    线性回归之电力预测
    python之产生随机数
  • 原文地址:https://www.cnblogs.com/immiao0319/p/13507950.html
Copyright © 2011-2022 走看看