Skip to main content

Posts

Showing posts with the label LeetCode Solutions

[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

[LeetCode Solution 39]: Combination Sum

[LeetCode Solution 39]:  Combination Sum ********************************************************************************* Question: Given a  set  of candidate numbers ( C )  (without duplicates)  and a target number ( T ), find all unique combinations in  C  where the candidate numbers sum to  T . The  same  repeated number may be chosen from  C  unlimited number of times. Note: All numbers (including the target) will be positive integers. The solution set must not contain duplicate combinations. For example, given candidate set  [2, 3, 6, 7]  and target, 7 A solution set is:  [ [7], [2, 2, 3] ] -------------------------------------------------------------------------------------------------- Approach Recursive Method Intuition Most problems like this, which requires the return of all the required solutions, can be solved by recursive and the thinking part is similar. if you carefully study these topics are found in a routine, are required to

[LeetCode Solution 230]: Kth Smallest Element in a BST

Question: Given a binary search tree, write a function  kthSmallest  to find the  k th smallest element in it. ************************************************************************************************************************************ Write Infront To read to a tutorial, please to read the tutorial of in-order traversal of BST, please check: LeetCode Solution 94: Binary Tree Inorder Traversal We are going to solve this question using the following 4 methods: ->Binary Search ->Recursive ->Iterative ->Morris  Approach #1 Binary Search [Accepted] Detail Explanation The first method to solve this problem is using Binary Search. The idea is very easy and extremely to think. We use BST's property that the left child of the root is smaller than the root while the right child of the root is always bigger. We consider that the root is the pivot, and find the number of the nodes in the left subtree and the number of the nodes in th

[LeetCode Solution 98] Validate Binary Search Tree

Question: Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node’s key. The right subtree of a node contains only nodes with keys greater than the node’s key. Both the left and right subtrees must also be binary search trees. Example 1 : 2 / \ 1 3 Binary tree [ 2 , 1 , 3 ], return true . Example 2 : 1 / \ 2 3 Binary tree [ 1 , 2 , 3 ], return false . 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 Write Before The problem is same to the BST Inorder Traversal:  LeetCode 94  !!! Why? Because the inorder traversal get the order from root.left -> root -> root.right, if the ValidBST is true, the inorder traversal is ascending order. How to implement inorder traversal in BST? ->Recursive Method ->Iterative Method ->Morris Method The tutorial