标签:bsp i++ ble possible oid amp start while substr
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"] ]
subtracking,差点就看透答案了呢,partition我想给你??头上来一刀
class Solution { public List<List<String>> partition(String s) { List<List<String>> result = new ArrayList<>(); List<String> path = new ArrayList<>(); // 一个partition方案 dfs(s, path, result, 0); return result; } // 搜索必须以s[start]开头的partition方案 private static void dfs(String s, List<String> path, List<List<String>> result, int start) { if (start == s.length()) { result.add(new ArrayList<>(path)); return; } for (int i = start; i < s.length(); i++) { if (isPalindrome(s, start, i)) { // 从i位置砍一刀 path.add(s.substring(start, i+1)); dfs(s, path, result, i + 1); // 继续往下砍 path.remove(path.size() - 1); // 撤销上上行 } } } private static boolean isPalindrome(String s, int start, int end) { while (start < end && s.charAt(start) == s.charAt(end)) { ++start; --end; } return start >= end; } }
标签:bsp i++ ble possible oid amp start while substr
原文地址:https://www.cnblogs.com/wentiliangkaihua/p/10516126.html