• [Leetcode] 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"]
      ]

    Solution:

     1 public class Solution {
     2     public List<List<String>> partition(String s) {
     3         List<List<String>> result=new ArrayList<List<String>>();
     4         List<String> temp=new ArrayList<String>();
     5         dfs(result,temp,s);
     6         return result;
     7     }
     8 
     9     private void dfs(List<List<String>> result, List<String> temp, String s) {
    10         // TODO Auto-generated method stub
    11         if(s.length()==0){
    12             result.add(new ArrayList<String>(temp));
    13             return;
    14         }
    15         for(int i=1;i<=s.length();++i){
    16             String str=s.substring(0, i);
    17             if(isPalindrome(str)){
    18                 temp.add(str);
    19                 dfs(result, temp, s.substring(i));
    20                 temp.remove(temp.size()-1);
    21             }
    22         }    
    23     }
    24 
    25     private boolean isPalindrome(String str) {
    26         // TODO Auto-generated method stub
    27         int end=str.length()-1;
    28         int start=0;
    29         while(start<end){
    30             if(str.charAt(start)!=str.charAt(end))
    31                 return false;
    32             start++;
    33             end--;
    34         }
    35         return true;
    36     }
    37 }
  • 相关阅读:
    Linux基本命令
    LR之流程
    Jmeter&Ant构建自动化测试平台
    正则表达式
    搭建wordpress-安装xshell
    git本地文件提交
    Git基本操作
    python-之基本语法
    SQL语句之-简单查询
    postman之请求&断言
  • 原文地址:https://www.cnblogs.com/Phoebe815/p/4096901.html
Copyright © 2020-2023  润新知