Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
luzhipeng committed Apr 23, 2019
1 parent 7fa9cc8 commit 283291c
Show file tree
Hide file tree
Showing 18 changed files with 1,090 additions and 6 deletions.
70 changes: 70 additions & 0 deletions 39.combination-sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* @lc app=leetcode id=39 lang=javascript
*
* [39] Combination Sum
*
* https://leetcode.com/problems/combination-sum/description/
*
* algorithms
* Medium (46.89%)
* Total Accepted: 326.7K
* Total Submissions: 684.2K
* Testcase Example: '[2,3,6,7]\n7'
*
* Given a set of candidate numbers (candidates) (without duplicates) and a
* target number (target), find all unique combinations in candidates where the
* candidate numbers sums to target.
*
* The same repeated number may be chosen from candidates unlimited number of
* times.
*
* Note:
*
*
* All numbers (including target) will be positive integers.
* The solution set must not contain duplicate combinations.
*
*
* Example 1:
*
*
* Input: candidates = [2,3,6,7], target = 7,
* A solution set is:
* [
* ⁠ [7],
* ⁠ [2,2,3]
* ]
*
*
* Example 2:
*
*
* Input: candidates = [2,3,5], target = 8,
* A solution set is:
* [
* [2,2,2,2],
* [2,3,3],
* [3,5]
* ]
*
*/

function backtrack(list, tempList, nums, remain, start) {
if (remain < 0) return;
else if (remain === 0) return list.push([...tempList]);
for (let i = start; i < nums.length; i++) {
tempList.push(nums[i]);
backtrack(list, tempList, nums, remain - nums[i], i); // 数字可以重复使用, i + 1代表不可以重复利用
tempList.pop();
}
}
/**
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
var combinationSum = function(candidates, target) {
const list = [];
backtrack(list, [], candidates.sort((a, b) => a - b), target, 0);
return list;
};
71 changes: 71 additions & 0 deletions 40.combination-sum-ii.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* @lc app=leetcode id=40 lang=javascript
*
* [40] Combination Sum II
*
* https://leetcode.com/problems/combination-sum-ii/description/
*
* algorithms
* Medium (40.31%)
* Total Accepted: 212.8K
* Total Submissions: 519K
* Testcase Example: '[10,1,2,7,6,1,5]\n8'
*
* Given a collection of candidate numbers (candidates) and a target number
* (target), find all unique combinations in candidates where the candidate
* numbers sums to target.
*
* Each number in candidates may only be used once in the combination.
*
* Note:
*
*
* All numbers (including target) will be positive integers.
* The solution set must not contain duplicate combinations.
*
*
* Example 1:
*
*
* Input: candidates = [10,1,2,7,6,1,5], target = 8,
* A solution set is:
* [
* ⁠ [1, 7],
* ⁠ [1, 2, 5],
* ⁠ [2, 6],
* ⁠ [1, 1, 6]
* ]
*
*
* Example 2:
*
*
* Input: candidates = [2,5,2,1,2], target = 5,
* A solution set is:
* [
* [1,2,2],
* [5]
* ]
*
*
*/
function backtrack(list, tempList, nums, remain, start) {
if (remain < 0) return;
else if (remain === 0) return list.push([...tempList]);
for (let i = start; i < nums.length; i++) {
if(i > start && nums[i] == nums[i-1]) continue; // skip duplicates
tempList.push(nums[i]);
backtrack(list, tempList, nums, remain - nums[i], i + 1); // i + 1代表不可以重复利用, i 代表数字可以重复使用
tempList.pop();
}
}
/**
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
var combinationSum2 = function(candidates, target) {
const list = [];
backtrack(list, [], candidates.sort((a, b) => a - b), target, 0);
return list;
};
50 changes: 50 additions & 0 deletions 46.permutations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* @lc app=leetcode id=46 lang=javascript
*
* [46] Permutations
*
* https://leetcode.com/problems/permutations/description/
*
* algorithms
* Medium (53.60%)
* Total Accepted: 344.6K
* Total Submissions: 642.9K
* Testcase Example: '[1,2,3]'
*
* Given a collection of distinct integers, return all possible permutations.
*
* Example:
*
*
* Input: [1,2,3]
* Output:
* [
* ⁠ [1,2,3],
* ⁠ [1,3,2],
* ⁠ [2,1,3],
* ⁠ [2,3,1],
* ⁠ [3,1,2],
* ⁠ [3,2,1]
* ]
*
*
*/
function backtrack(list, tempList, nums) {
if (tempList.length === nums.length) return list.push([...tempList]);
for(let i = 0; i < nums.length; i++) {
if (tempList.includes(nums[i])) continue;
tempList.push(nums[i]);
backtrack(list, tempList, nums);
tempList.pop();
}
}
/**
* @param {number[]} nums
* @return {number[][]}
*/
var permute = function(nums) {
const list = [];
backtrack(list, [], nums)
return list
};

52 changes: 52 additions & 0 deletions 47.permutations-ii.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* @lc app=leetcode id=47 lang=javascript
*
* [47] Permutations II
*
* https://leetcode.com/problems/permutations-ii/description/
*
* algorithms
* Medium (39.29%)
* Total Accepted: 234.1K
* Total Submissions: 586.2K
* Testcase Example: '[1,1,2]'
*
* Given a collection of numbers that might contain duplicates, return all
* possible unique permutations.
*
* Example:
*
*
* Input: [1,1,2]
* Output:
* [
* ⁠ [1,1,2],
* ⁠ [1,2,1],
* ⁠ [2,1,1]
* ]
*
*
*/
function backtrack(list, nums, tempList, visited) {
if (tempList.length === nums.length) return list.push([...tempList]);
for (let i = 0; i < nums.length; i++) {
if (visited[i]) continue;
// visited[i - 1] 容易忽略
if (i > 0 && nums[i] === nums[i - 1] && visited[i - 1]) continue;

visited[i] = true;
tempList.push(nums[i]);
backtrack(list, nums, tempList, visited);
visited[i] = false;
tempList.pop();
}
}
/**
* @param {number[]} nums
* @return {number[][]}
*/
var permuteUnique = function(nums) {
const list = [];
backtrack(list, nums.sort((a, b) => a - b), [], []);
return list;
};
71 changes: 71 additions & 0 deletions 62.unique-paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* @lc app=leetcode id=62 lang=javascript
*
* [62] Unique Paths
*
* https://leetcode.com/problems/unique-paths/description/
*
* algorithms
* Medium (46.53%)
* Total Accepted: 277K
* Total Submissions: 587.7K
* Testcase Example: '3\n2'
*
* A robot is located at the top-left corner of a m x n grid (marked 'Start' in
* the diagram below).
*
* The robot can only move either down or right at any point in time. The robot
* is trying to reach the bottom-right corner of the grid (marked 'Finish' in
* the diagram below).
*
* How many possible unique paths are there?
*
*
* Above is a 7 x 3 grid. How many possible unique paths are there?
*
* Note: m and n will be at most 100.
*
* Example 1:
*
*
* Input: m = 3, n = 2
* Output: 3
* Explanation:
* From the top-left corner, there are a total of 3 ways to reach the
* bottom-right corner:
* 1. Right -> Right -> Down
* 2. Right -> Down -> Right
* 3. Down -> Right -> Right
*
*
* Example 2:
*
*
* Input: m = 7, n = 3
* Output: 28
*
* START
*/
/**
* @param {number} m
* @param {number} n
* @return {number}
*/
var uniquePaths = function(m, n) {
// backtracking
const dp = [];
for (let i = 0; i < m + 1; i++) {
dp[i] = [];
dp[i][0] = 0;
}
for (let i = 0; i < n + 1; i++) {
dp[0][i] = 0;
}
for (let i = 1; i < m + 1; i++) {
for(let j = 1; j < n + 1; j++) {
dp[i][j] = j === 1 ? 1 : dp[i - 1][j] + dp[i][j - 1];
}
}

return dp[m][n];
};
53 changes: 53 additions & 0 deletions 78.subsets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* @lc app=leetcode id=78 lang=javascript
*
* [78] Subsets
*
* https://leetcode.com/problems/subsets/description/
*
* algorithms
* Medium (51.19%)
* Total Accepted: 351.6K
* Total Submissions: 674.8K
* Testcase Example: '[1,2,3]'
*
* Given a set of distinct integers, nums, return all possible subsets (the
* power set).
*
* Note: The solution set must not contain duplicate subsets.
*
* Example:
*
*
* Input: nums = [1,2,3]
* Output:
* [
* ⁠ [3],
* [1],
* [2],
* [1,2,3],
* [1,3],
* [2,3],
* [1,2],
* []
* ]
*
*/
function backtrack(list, tempList, nums, start) {
list.push([...tempList]);
for(let i = start; i < nums.length; i++) {
tempList.push(nums[i]);
backtrack(list, tempList, nums, i + 1);
tempList.pop();
}
}
/**
* @param {number[]} nums
* @return {number[][]}
*/
var subsets = function(nums) {
const list = [];
backtrack(list, [], nums, 0);
return list;
};

Loading

0 comments on commit 283291c

Please sign in to comment.