Description
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
Example:
1 | Input: "aab" |
解法
经验:遇到要求所有组合、可能、排列等解集的题目,一般都是DFS + backtracking
- 首先传入s=”aab” path=[] res = [], 首先切割出”a”(然后是”aa” “aab” …),然后判读它是不是回文串:
- 如果不是,直接跳过
- 如果是,则此时剩余的 s=”ab”, path += [“a”]
- 写入res的判断是,当s=””时,记录结果
具体代码如下:
1 | class Solution { |