Skip to main content

Posts

Showing posts from October, 2017

[LeetCode Solution 131]: Palindrome Partitioning

[LeetCode Solution 131]: Palindrome Partitioning ********************************************************************************* Question: 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"] ] -------------------------------------------------------------------------------------------------- Recursive Method Strategy We are going to use backtracking to solve this problem, the idea is if we find a palindrome  substring, we add it in a temp and go further searching, the idea is as shows in the following picture: Java public class Solution { public List<List<String>> partition(String s) { if (s.length() == 0 || s == null) return null; List<List<String>> res = new ArrayList&

[LeetCode Solution 40]: Combination Sum II

[LeetCode Solution 40]: Combination Sum II ********************************************************************************* Question: Given a collection of candidate numbers ( C ) and a target number ( T ), find all unique combinations in  C  where the candidate numbers sums to  T . Each number in  C  may only be used  once  in the combination. Note: All numbers (including the target) will be positive integers. The solution set must not contain duplicate combinations. For example, given candidate set  [10, 1, 2, 7, 6, 1, 5]  and target, 8 A solution set is:  [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ] -------------------------------------------------------------------------------------------------- Approach Recursive Method Strategy The idea is same with the  [LeetCode Solution 39] Combination Sum , using backtracking method, but the only difference is how to get the value that we have to skip that can help us to avoid the duplication. H