zoukankan      html  css  js  c++  java
  • [leedcode 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 class Solution {
        //DFS思想,getPart函数的意思是从s的start索引处开始求回文,DFS过程是,取start的i个字符,如果是回文,保存结果,从start+i开始遍历
        List<List<String>> res;
        List<String> seq;
        public List<List<String>> partition(String s) {
            res=new ArrayList<List<String>>();
            seq=new ArrayList<String>();
            getPart(s,0);
            return res;
        }
        public void getPart(String s,int start){
            if(start==s.length()){
                res.add(new ArrayList<String>(seq));
            }
            for(int i=start+1;i<=s.length();i++){
                if(isPart(s.substring(start,i))){
                    seq.add(s.substring(start,i));
                    getPart(s,i);
                    seq.remove(seq.size()-1);
                }
            }
        }
        public boolean isPart(String s){
            int i=0;
            int j=s.length()-1;
            while(i<j){
                if(s.charAt(i)!=s.charAt(j))return false;
                i++;
                j--;
            }
            return true;
        }
        
        
    }
  • 相关阅读:
    JS函数防抖与函数节流
    AJAX问题 XMLHttpRequest.status = 0是什么含义
    通过JS如何获取IP地址
    关于URL编码
    报错Unexpected token u
    css文本超出2行就隐藏并且显示省略号
    At_speed_test
    Logic Bist Arch
    Logic BIST
    DMA-330(二)
  • 原文地址:https://www.cnblogs.com/qiaomu/p/4677557.html
Copyright © 2011-2022 走看看