zoukankan      html  css  js  c++  java
  • Palindrome Partitioning

    注意add arraylist<E> 到 ArrayList<ArrayList<E>>的时候传递引用和传值得区别!!!

     1 public class Solution {
     2     public ArrayList<ArrayList<String>> partition(String s) {
     3         // IMPORTANT: Please reset any member data you declared, as
     4         // the same Solution instance will be reused for each test case.
     5         ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();
     6         if(s == null || s.length() == 0)
     7             return result;
     8         ArrayList<String> stringList = new ArrayList<String>();
     9         PaPartition(s, 0, s.length(), stringList, result);
    10         return result;
    11     }
    12     private boolean isPalindrome(String s, int start, int end)
    13     {
    14         while(start < end - 1)
    15         {
    16             if(s.charAt(start) != s.charAt(end - 1))
    17                 return false;
    18             start ++;
    19             end --;
    20         }
    21         return true;
    22     }
    23     private void PaPartition(String s, int start, int end, ArrayList<String> stringList, ArrayList<ArrayList<String>> result)
    24     {
    25         if(start >= end)
    26         {
    27             ArrayList<String> tmp = new ArrayList<String>();
    28        tmp.addAll(stringList);
    29             result.add(tmp);
    30             return;
    31         }
    32         for(int i = start + 1; i <= end; i++)
    33         {
    34             if(isPalindrome(s, start, i))
    35             {
    36                 stringList.add(s.substring(start, i));
    37                 PaPartition(s, i, end, stringList, result);
    38                 stringList.remove(stringList.size() - 1);
    39             }
    40         }
    41     }
    42 }
  • 相关阅读:
    envoy部分二: envoy的配置组件
    envoy部分一:enovy基础
    envoy部分四:envoy的静态配置
    envoy部分七:envoy的http流量管理基础
    envoy部分六:envoy的集群管理
    十七、Kubernetes的网络管理模型
    SQL 日期时间函数
    JSON 和 JavaScript eval
    Ajax 读取.ashx 返回404
    Repeat 数据为空时的处理
  • 原文地址:https://www.cnblogs.com/jasonC/p/3417373.html
Copyright © 2011-2022 走看看