diff --git a/114 Flatten Binary Tree to Linked List.js b/114 Flatten Binary Tree to Linked List.js index 2f68381..1b20145 100644 --- a/114 Flatten Binary Tree to Linked List.js +++ b/114 Flatten Binary Tree to Linked List.js @@ -16,12 +16,12 @@ var flatten = function(root) { var stack = []; var p = root; - + while(p !== null || stack.length !== 0){ if(p.right !== null){ stack.push(p.right); } - + if(p.left !== null){ // [!!!]point of confusing, if null then pop stack p.right = p.left; p.left = null; @@ -29,8 +29,31 @@ var flatten = function(root) { var node = stack.pop(); p.right = node; } - + p = p.right; } }; +// Recursive solution + +var flatten = function(root) { + if(root === null || (root.left === null && root.right === null)) { + return; + } + + var rootLeft = root.left; + var rootRight = root.right; + root.left = null; + root.right = null; + + flatten(rootLeft); + flatten(rootRight); + + root.right = rootLeft; + + var aux = root; + while(aux !== null && aux.right !== null) { + aux = aux.right; + } + aux.right = rootRight; +}; diff --git a/153 Find Minimum in Rotated Sorted Array.js b/153 Find Minimum in Rotated Sorted Array.js index 7e98c2f..a01c797 100644 --- a/153 Find Minimum in Rotated Sorted Array.js +++ b/153 Find Minimum in Rotated Sorted Array.js @@ -10,10 +10,10 @@ var findMin = function(nums) { var s = 0; var e = nums.length - 1; var min; - + while(s nums[e]) { @@ -21,8 +21,17 @@ var findMin = function(nums) { } else { return nums[s]; } - + } - + return Math.min(nums[e], nums[s]); -}; \ No newline at end of file +}; + +// Another shorter solution; +var findMin = function(nums) { + var i = 0; + while(i < nums.length - 1 && nums[i] < nums[i + 1]) { + i++; + } + return (i === nums.length - 1)? nums[0] : nums[i + 1] +}; diff --git a/39 Combination Sum.js b/39 Combination Sum.js index e8214d3..27e2607 100644 --- a/39 Combination Sum.js +++ b/39 Combination Sum.js @@ -5,8 +5,8 @@ // Note: // All numbers (including 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: +// For example, given candidate set [2, 3, 6, 7] and target 7, +// A solution set is: // [ // [7], // [2, 2, 3] @@ -24,37 +24,61 @@ */ var combinationSum = function(candidates, target) { var result = []; - + if(candidates === null || candidates.length === 0){ return result; } - + candidates.sort(function(a,b){return a > b ? 1 : -1}); - + var output = []; - + generate(candidates, result, output, target, 0); - + return result; }; var generate = function(candidates, result, output, sum, index){ if(sum === 0){ - result.push(output.slice()); + result.push(output.slice()); } if(sum < 0){ return; } - + for(var i = index; i < candidates.length; i++){ if(i > index && candidates[i] === candidates[i - 1]){ continue; } - + if(candidates[i] <= sum){ output.push(candidates[i]); generate(candidates, result, output, sum - candidates[i], i); output.pop(); } } -} \ No newline at end of file +} + + +// Another solution +var combinationSum = function(candidates, target) { + var results = []; + comb(candidates.sort(), 0, [], 0, target, results); + return results; +}; + +var comb = function(cand, index, partial, partialSum, target, results) { + if(target === partialSum) { + results.push(partial); + return; + } + if(cand.length === index || partialSum > target) { + return; + } + + comb(cand, index + 1, partial, partialSum, target, results); + comb(cand, index, partial.concat([cand[index]].concat([cand[index]])), + partialSum + 2* cand[index], target, results); + comb(cand, index + 1, partial.concat([cand[index]]), + partialSum + cand[index], target, results); +}; diff --git a/47 Permutations II.js b/47 Permutations II.js index 3bb9f3b..5bb29fa 100644 --- a/47 Permutations II.js +++ b/47 Permutations II.js @@ -50,4 +50,32 @@ var generatePermute = function(nums, step, currentResult, visited, finalResult) visited[i] = false; } } -} \ No newline at end of file +} + +//Another Solution, similar approach that Permutation.js +var permuteUnique = function(nums) { + return permut(nums.sort(), []); +}; + +var permut = function(nums, partial) { + if(nums.length === 0) { + return [partial]; + } + var listSol = []; + for(var i = 0; i < nums.length; i++) { + var endRepeated = i; + while(endRepeated < nums.length && nums[i] === nums[endRepeated]) { + endRepeated++; + } + + var arrayWithoutI = nums.slice(0,i).concat(nums.slice(i + 1, nums.length)); + var partialSol = partial.concat([nums[i]]); + var sol = permut(arrayWithoutI, partialSol); + if(sol.length > 0){ + listSol = listSol.concat(sol); + } + i = endRepeated - 1; + } + return listSol; +}; + diff --git a/48 Rotate Image.js b/48 Rotate Image.js index 68e2819..546ab53 100644 --- a/48 Rotate Image.js +++ b/48 Rotate Image.js @@ -11,20 +11,20 @@ */ var rotate = function(matrix) { var row = matrix.length; - + if(row === 0) { return; } - + var col = matrix[0].length; - + // swap them in diagonal for(var i = 0; i < row; i++) { for(var j = 0; j < col - i; j++) { swap(matrix, i, j, row - 1 - j, col - 1 - i); } } - + // swap in middle for(i = 0; i < Math.floor(row/2); i++) { for(j = 0; j < col; j++) { @@ -37,4 +37,37 @@ function swap(matrix, x1, y1, x2, y2) { var tmp = matrix[x1][y1]; matrix[x1][y1] = matrix[x2][y2]; matrix[x2][y2] = tmp; -} \ No newline at end of file +} + +//Clearer Solution +var rotate = function(matrix) { + rotateColumns(matrix); + rotateEachDiagonal(matrix); +}; + +var rotateColumns = function(matrix) { + for(var j = 0; j < matrix.length; j++) { + var low = 0; + var ceil = matrix.length -1; + while(low < ceil) { + swap(matrix, low, j, ceil, j); + low++; + ceil--; + } + } +}; + +var rotateEachDiagonal = function(matrix){ + for(var i = 0; i < matrix.length; i++) { + for(var j = i; j < matrix.length; j++) { + swap(matrix, i, j, j, i); + } + } +}; + +var swap = function(matrix, i1, j1, i2, j2) { + var aux = matrix[i1][j1]; + matrix[i1][j1] = matrix[i2][j2]; + matrix[i2][j2] = aux; +}; + diff --git a/78 Subsets.js b/78 Subsets.js index 9e35ac8..7540086 100644 --- a/78 Subsets.js +++ b/78 Subsets.js @@ -32,7 +32,7 @@ var generate = function(nums, index, cur, result) { result.push(cur.slice()); return } - + generate(nums, index + 1, cur, result); cur.push(nums[index]); generate(nums, index + 1, cur, result); @@ -42,11 +42,9 @@ var generate = function(nums, index, cur, result) { // second try - - var subsets = function(nums) { var res = [[]]; - + function generate(nums, i, cur, res) { for(; i < nums.length; i++) { cur.push(nums[i]); @@ -55,7 +53,24 @@ var subsets = function(nums) { cur.pop(); } } - + generate(nums, 0, [], res); return res; -}; \ No newline at end of file +}; + +// Third solution +var subsets = function(nums) { + var results = []; + combine(nums, 0, [], results); + return results; +} + +var combine = function(nums, index, partial, results) { + if(index === nums.length) { + results.push(partial); + return; + } + + combine(nums, index + 1, partial, results); + combine(nums, index + 1, partial.concat([nums[index]]), results); +}