Skip to content

Commit 8d8c5cc

Browse files
author
yupeng10
committed
update
1 parent e64d97c commit 8d8c5cc

13 files changed

+2180
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
2+
//
3+
// 给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
4+
//
5+
//
6+
//
7+
//
8+
//
9+
// 示例 1:
10+
//
11+
//
12+
//输入:digits = "23"
13+
//输出:["ad","ae","af","bd","be","bf","cd","ce","cf"]
14+
//
15+
//
16+
// 示例 2:
17+
//
18+
//
19+
//输入:digits = ""
20+
//输出:[]
21+
//
22+
//
23+
// 示例 3:
24+
//
25+
//
26+
//输入:digits = "2"
27+
//输出:["a","b","c"]
28+
//
29+
//
30+
//
31+
//
32+
// 提示:
33+
//
34+
//
35+
// 0 <= digits.length <= 4
36+
// digits[i] 是范围 ['2', '9'] 的一个数字。
37+
//
38+
//
39+
// Related Topics 哈希表 字符串 回溯 👍 2971 👎 0
40+
41+
42+
package leetcode.editor.cn;
43+
44+
import java.util.ArrayList;
45+
import java.util.HashMap;
46+
import java.util.List;
47+
import java.util.Map;
48+
49+
public class P17LetterCombinationsOfAPhoneNumber {
50+
public static void main(String[] args) {
51+
new P17LetterCombinationsOfAPhoneNumber().new Solution().letterCombinations("23");
52+
}
53+
54+
//leetcode submit region begin(Prohibit modification and deletion)
55+
class Solution {
56+
public List<String> letterCombinations(String digits) {
57+
List<String> combinations = new ArrayList<String>();
58+
if (digits.length() == 0) {
59+
return combinations;
60+
}
61+
Map<Character, String> phoneMap = new HashMap<Character, String>() {{
62+
put('2', "abc");
63+
put('3', "def");
64+
put('4', "ghi");
65+
put('5', "jkl");
66+
put('6', "mno");
67+
put('7', "pqrs");
68+
put('8', "tuv");
69+
put('9', "wxyz");
70+
}};
71+
backtrack(combinations, phoneMap, 0, digits, new StringBuilder(""));
72+
return combinations;
73+
}
74+
75+
private void backtrack(List<String> combinations, Map<Character, String> phoneMap, int index, String digits, StringBuilder stringBuilder) {
76+
if (index == digits.length()) {
77+
combinations.add(stringBuilder.toString());
78+
return;
79+
}
80+
81+
char digit = digits.charAt(index);
82+
String s = phoneMap.get(digit);
83+
for (int i = 0; i < s.length(); i++) {
84+
stringBuilder.append(s.charAt(i));
85+
backtrack(combinations, phoneMap, index + 1, digits, stringBuilder);
86+
stringBuilder.deleteCharAt(index);
87+
}
88+
89+
}
90+
}
91+
//leetcode submit region end(Prohibit modification and deletion)
92+
93+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。
2+
//
3+
// 解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。
4+
//
5+
//
6+
//
7+
// 示例 1:
8+
//
9+
//
10+
//输入:nums = [1,2,3]
11+
//输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
12+
//
13+
//
14+
// 示例 2:
15+
//
16+
//
17+
//输入:nums = [0]
18+
//输出:[[],[0]]
19+
//
20+
//
21+
//
22+
//
23+
// 提示:
24+
//
25+
//
26+
// 1 <= nums.length <= 10
27+
// -10 <= nums[i] <= 10
28+
// nums 中的所有元素 互不相同
29+
//
30+
//
31+
// Related Topics 位运算 数组 回溯 👍 2400 👎 0
32+
33+
34+
package leetcode.editor.cn;
35+
36+
import java.util.ArrayList;
37+
import java.util.List;
38+
39+
public class P78Subsets{
40+
public static void main(String[] args) {
41+
new P78Subsets().new Solution().subsets(new int[]{1,2,3});
42+
}
43+
//leetcode submit region begin(Prohibit modification and deletion)
44+
class Solution {
45+
List<Integer> t = new ArrayList<>();
46+
List<List<Integer>> res = new ArrayList<>();
47+
// public List<List<Integer>> subsets(int[] nums) {
48+
// int n = nums.length;
49+
// //表示二进制
50+
// for (int mask = 0; mask < (1 << n); mask++) {
51+
// t.clear();
52+
// for (int i = 0; i < n; i++) {
53+
// if ((mask & (1 << i)) != 0) {
54+
// t.add(nums[i]);
55+
// }
56+
// }
57+
// res.add(new ArrayList<>(t));
58+
// }
59+
// return res;
60+
// }
61+
62+
public List<List<Integer>> subsets(int[] nums) {
63+
int n = nums.length;
64+
//表示二进制
65+
66+
return res;
67+
}
68+
}
69+
//leetcode submit region end(Prohibit modification and deletion)
70+
71+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<p>给定一个仅包含数字&nbsp;<code>2-9</code>&nbsp;的字符串,返回所有它能表示的字母组合。答案可以按 <strong>任意顺序</strong> 返回。</p>
2+
3+
<p>给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。</p>
4+
5+
<p><img src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2021/11/09/200px-telephone-keypad2svg.png" style="width: 200px;" /></p>
6+
7+
<p>&nbsp;</p>
8+
9+
<p><strong>示例 1:</strong></p>
10+
11+
<pre>
12+
<strong>输入:</strong>digits = "23"
13+
<strong>输出:</strong>["ad","ae","af","bd","be","bf","cd","ce","cf"]
14+
</pre>
15+
16+
<p><strong>示例 2:</strong></p>
17+
18+
<pre>
19+
<strong>输入:</strong>digits = ""
20+
<strong>输出:</strong>[]
21+
</pre>
22+
23+
<p><strong>示例 3:</strong></p>
24+
25+
<pre>
26+
<strong>输入:</strong>digits = "2"
27+
<strong>输出:</strong>["a","b","c"]
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
32+
<p><strong>提示:</strong></p>
33+
34+
<ul>
35+
<li><code>0 &lt;= digits.length &lt;= 4</code></li>
36+
<li><code>digits[i]</code> 是范围 <code>['2', '9']</code> 的一个数字。</li>
37+
</ul>
38+
39+
<div><div>Related Topics</div><div><li>哈希表</li><li>字符串</li><li>回溯</li></div></div><br><div><li>👍 2971</li><li>👎 0</li></div>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<p>给你一个整数数组&nbsp;<code>nums</code> ,数组中的元素 <strong>互不相同</strong> 。返回该数组所有可能的<span data-keyword="subset">子集</span>(幂集)。</p>
2+
3+
<p>解集 <strong>不能</strong> 包含重复的子集。你可以按 <strong>任意顺序</strong> 返回解集。</p>
4+
5+
<p>&nbsp;</p>
6+
7+
<p><strong>示例 1:</strong></p>
8+
9+
<pre>
10+
<strong>输入:</strong>nums = [1,2,3]
11+
<strong>输出:</strong>[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
12+
</pre>
13+
14+
<p><strong>示例 2:</strong></p>
15+
16+
<pre>
17+
<strong>输入:</strong>nums = [0]
18+
<strong>输出:</strong>[[],[0]]
19+
</pre>
20+
21+
<p>&nbsp;</p>
22+
23+
<p><strong>提示:</strong></p>
24+
25+
<ul>
26+
<li><code>1 &lt;= nums.length &lt;= 10</code></li>
27+
<li><code>-10 &lt;= nums[i] &lt;= 10</code></li>
28+
<li><code>nums</code> 中的所有元素 <strong>互不相同</strong></li>
29+
</ul>
30+
31+
<div><div>Related Topics</div><div><li>位运算</li><li>数组</li><li>回溯</li></div></div><br><div><li>👍 2400</li><li>👎 0</li></div>
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
> Problem: [78. 子集](https://leetcode.cn/problems/subsets/description/)
2+
3+
[TOC]
4+
5+
# 输入视角
6+
7+
## 思路
8+
9+
'''
10+
子集型回溯:每个元素都可以选/不选
11+
12+
时间复杂度O(n2^n), n为nums的长度。由于每个元素都是选或者不选,递归次数为一共满二叉树的节点个数
13+
一共会递归O(2^n)次,再算入加入答案时,复制道path需要O(n)的时间,所以时间复杂度为O(n2^n)
14+
空间复杂度O(n)
15+
'''
16+
17+
## Code
18+
19+
* []
20+
21+
```Python
22+
class Solution:
23+
def subsets(self, nums: List[int]) -> List[List[int]]:
24+
'''
25+
子集型回溯:每个元素都可以选/不选
26+
27+
时间复杂度O(n2^n), n为nums的长度。由于每个元素都是选或者不选,递归次数为一共满二叉树的节点个数
28+
一共会递归O(2^n)次,再算入加入答案时,复制道path需要O(n)的时间,所以时间复杂度为O(n2^n)
29+
空间复杂度O(n)
30+
'''
31+
ans = []
32+
path = []
33+
n = len(nums)
34+
35+
def dfs(i: int) -> None:
36+
# dfs(i)中的i表示当前nums[i]选或者不选
37+
if i == n: # 子集构造完毕
38+
ans.append(path.copy()) # 复制 path,也可以写 path[:]
39+
return
40+
41+
# 不选 nums[i]
42+
dfs(i + 1)
43+
44+
# 选 nums[i]
45+
path.append(nums[i])
46+
dfs(i + 1)
47+
path.pop() # 恢复现场
48+
49+
dfs(0)
50+
return ans
51+
```
52+
53+
# 答案视角
54+
55+
## 思路
56+
57+
'''
58+
答案视角,枚举答案的第一个数选谁,第二个数选谁,以此类推
59+
dfs中的i表示要枚举选nums[i]到nums[n-1]中的一个数,添加到path末尾
60+
如果选nums[j]添加到path末尾,那么下一个要添加到path末尾的数,
61+
就要在nums[j+1]到nums[n-1]中枚举了
62+
63+
64+
时间复杂度O(n2^n),n为nums的长度。答案的长度为子集的个数,即2^n
65+
每次递归都把一个数组放入答案,因此会递归2^n次,再算上加入答案时复制path需要O(n)
66+
的时间,所以时间复杂度为O(n2^n)
67+
空间复杂度O(n)
68+
'''
69+
70+
## Code
71+
72+
* []
73+
74+
```Python
75+
class Solution:
76+
def subsets(self, nums: List[int]) -> List[List[int]]:
77+
'''
78+
答案视角,枚举答案的第一个数选谁,第二个数选谁,以此类推
79+
dfs中的i表示要枚举选nums[i]到nums[n-1]中的一个数,添加到path末尾
80+
如果选nums[j]添加到path末尾,那么下一个要添加到path末尾的数,
81+
就要在nums[j+1]到nums[n-1]中枚举了
82+
83+
84+
时间复杂度O(n2^n),n为nums的长度。答案的长度为子集的个数,即2^n
85+
每次递归都把一个数组放入答案,因此会递归2^n次,再算上加入答案时复制path需要O(n)
86+
的时间,所以时间复杂度为O(n2^n)
87+
空间复杂度O(n)
88+
'''
89+
ans = []
90+
path = []
91+
n = len(nums)
92+
93+
def dfs(i):
94+
ans.append(path.copy())
95+
96+
if i == n:
97+
return
98+
99+
for j in range(i, n):
100+
path.append(nums[j])
101+
dfs(j+1)
102+
path.pop() # 恢复现场
103+
104+
dfs(0)
105+
return ans
106+
107+
108+
```
109+

0 commit comments

Comments
 (0)