[LeetCode][JavaScript]Subsets

Subsets

Given a set of distinct integers, nums, return all possible subsets.

Note:

  • Elements in a subset must be in non-descending order.
  • The solution set must not contain duplicate subsets.

For example,
If nums = [1,2,3], a solution is:

[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]


全排列。

首先是非递归的巧妙解法。

比如题目中的例子[1,2,3]:

一共有8种情况,如果用三位的二进制表示,1代表选,0代表不选,正好就是000到111。

代码中的padLeft方法把当前的数转化成长度等于nums.length的二进制字符串,位数不足以0补齐。

 1 /**
 2  * @param {number[]} nums
 3  * @return {number[][]}
 4  */
 5 var subsets = function(nums) {
 6     nums = nums.sort(sorting);
 7     var res = [];
 8     for(var i = 0; i < Math.pow(2, nums.length); i++){
 9         var str = padLeft(i, nums.length);
10         var tmp = [];
11         for(var j = 0; j < str.length; j++){
12             if(str[j] === ‘1‘){
13                 tmp.push(nums[j]);
14             }
15         }
16         res.push(tmp);
17     }
18     return res;
19
20     function padLeft(num, len){
21         var res = "", i = len;
22         while(i--) res += ‘0‘;
23         var tmp = parseInt(num).toString(2);
24         res = res + tmp;
25         return res.substring(tmp.length, res.length);
26     }
27
28     function sorting(a, b){
29         if(a > b){
30             return 1;
31         }else if(a < b){
32             return -1;
33         }else{
34             return 0;
35         }
36     }
37 };

效率很高。

常规的递归解法。

比较难以描述,举栗子[1,2,3,4]

当递归到[1,2]的时候,下一轮的递归是[1,2,3]和[1,2,4]。

[1,2,3]又会递归到[1,2,3,4]。

当递归到[1,3]的时候,下一轮的递归是[1,3,4]。

以此类推。

 1 /**
 2  * @param {number[]} nums
 3  * @return {number[][]}
 4  */
 5 var subsets = function(nums) {
 6     nums = nums.sort(sorting);
 7     var res = [[]], arr = [];
 8     for(var i = 0; i < nums.length; i++){
 9         res.push([nums[i]]);
10         arr.push({val : [nums[i]], pos : i});
11     }
12     getSets(arr);
13     return res;
14
15     function getSets(arr){
16         var i, j, tmp, nextArr = [];
17         for(i = 0; i < arr.length; i++){
18             for(j = arr[i].pos + 1; j < nums.length; j++){
19                 tmp = arr[i].val.slice(0);
20                 tmp.push(nums[j]);
21                 res.push(tmp);
22                 nextArr.push({val : tmp, pos : j});
23             }
24         }
25         if(nextArr.length > 0){
26             getSets(nextArr);
27         }
28     }
29
30     function sorting(a, b){
31         if(a > b){
32             return 1;
33         }else if(a < b){
34             return -1;
35         }else{
36             return 0;
37         }
38     }
39 }
时间: 2024-07-30 10:07:48

[LeetCode][JavaScript]Subsets的相关文章

[LeetCode][JavaScript]Subsets II

Subsets II Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If nums = [1,2,2]

LeetCode OJ - Subsets 1 &amp;&amp; 2

这道题的做法,一定得掌握啊!!!  elegant & beautiful & concise 下面是AC代码: 1 /** 2 * Given a set of distinct integers, S, return all possible subsets. 3 * 这道题的做法应该要记住!!!!! 4 * @param s 5 * @return 6 */ 7 public ArrayList<ArrayList<Integer>> subsets(int[

LeetCode --- 90. Subsets II

题目链接:Subsets II Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2]

[LeetCode][JavaScript]Pascal&#39;s Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] https://leetcode.com/problems/pascals-triangle/ 杨辉三角. 每行第一个和最后一个是1,其余是pre[i - 1] +

Java for LeetCode 090 Subsets II

Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If nums = [1,2,2], a soluti

【LeetCode】Subsets II 解题报告

[题目] Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solutio

[leetcode]90. Subsets II数组子集(有重)

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 题意: 是的[leetcode

[LeetCode] 90.Subsets II tag: backtracking

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Example: Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 这个题目思路

LeetCode 90. Subsets II (子集合之二)

Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: The solution set must not contain duplicate subsets. For example,If nums = [1,2,2], a solution is: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 题目标签:Arr