Skip to content

Commit 7657030

Browse files
authored
Merge pull request algorithm004-01#401 from tifazxy/master
106-Week 02
2 parents 7486c24 + 2e3a23b commit 7657030

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import java.util.HashMap;
2+
import java.util.ArrayList;
3+
/*
4+
* @lc app=leetcode id=17 lang=java
5+
*
6+
* [17] Letter Combinations of a Phone Number
7+
*
8+
* https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/
9+
*
10+
* algorithms
11+
* Medium (43.31%)
12+
* Likes: 2720
13+
* Dislikes: 343
14+
* Total Accepted: 466.7K
15+
* Total Submissions: 1.1M
16+
* Testcase Example: '"23"'
17+
*
18+
* Given a string containing digits from 2-9 inclusive, return all possible
19+
* letter combinations that the number could represent.
20+
*
21+
* A mapping of digit to letters (just like on the telephone buttons) is given
22+
* below. Note that 1 does not map to any letters.
23+
*
24+
*
25+
*
26+
* Example:
27+
*
28+
*
29+
* Input: "23"
30+
* Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
31+
*
32+
*
33+
* Note:
34+
*
35+
* Although the above answer is in lexicographical order, your answer could be
36+
* in any order you want.
37+
*
38+
*/
39+
40+
// @lc code=start
41+
class Solution {
42+
43+
public List<String> letterCombinations(String digits) {
44+
HashMap<Character,String> map = new HashMap<Character,String>();
45+
map.put('2', "abc");
46+
map.put('3', "def");
47+
map.put('4', "ghi");
48+
map.put('5', "jkl");
49+
map.put('6', "mno");
50+
map.put('7', "pqrs");
51+
map.put('8', "tuv");
52+
map.put('9', "wxyz");
53+
54+
ArrayList<String> result = new ArrayList<String>();
55+
56+
if("".equals(digits))
57+
return result;
58+
else{
59+
_combaination(0,"",digits,map,result);
60+
}
61+
return result;
62+
}
63+
64+
private void _combaination( int level,
65+
String s,
66+
String digits,
67+
HashMap<Character,String> map,
68+
ArrayList<String> result){
69+
//terminator
70+
if(level==digits.length()){
71+
result.add(s);
72+
return;
73+
}
74+
//current process
75+
String chars = map.get(digits.charAt(level));
76+
for(int index = 0; index< chars.length();index++){
77+
//drill down
78+
_combaination(level+1,s+chars.charAt(index), digits, map, result);
79+
}
80+
81+
//reverse
82+
}
83+
}
84+
// @lc code=end
85+
//定义代码表的对应关系,对每一位数字可能对应的字母进行递归的排列
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @lc app=leetcode id=46 lang=java
3+
*
4+
* [46] Permutations
5+
*/
6+
7+
// @lc code=start
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
class Solution {
11+
public List<List<Integer>> permute(int[] nums) {
12+
List<List<Integer>> result = new ArrayList<List<Integer>>();
13+
_permutations(new int[nums.length], nums, new ArrayList<Integer>(), result);
14+
return result;
15+
}
16+
17+
private void _permutations( int[] visited,
18+
int[] nums,
19+
ArrayList<Integer> current,
20+
List<List<Integer>> result){
21+
//terminator
22+
if (current.size() == nums.length){
23+
result.add(new ArrayList<>(current));
24+
return;
25+
}
26+
//current process
27+
for(int i = 0; i < nums.length; i++){
28+
if(visited[i]==1){continue;}
29+
visited[i]=1;
30+
current.add(nums[i]);
31+
//drill down
32+
_permutations(visited, nums, current, result);
33+
//reverse
34+
visited[i]=0;
35+
current.remove(current.size()-1);
36+
}
37+
38+
}
39+
}
40+
// @lc code=end
41+
//全排列 回溯方式,每一位置一个标志位,如果是1 ,不在加入自己,如果不是加入自己并置为1

Week 02/id_106/NOTE.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
11
# NOTE
2+
20191021-1027 week2
3+
1.本周主要学习了hashmap tree等数据结构的基础概念。
4+
2.学习了递归的思想与编写模板,同时学习了分治回溯的问题解决思想。
5+
3.其中树作为二维数据结构,遍历方式主要使用递归的方式,递归编写是需要注意不能使用人脑递归的方式,同时要注意寻找重复子问题与数学归纳法的证明。
6+
4.递归的编写模板范式如下
7+
public void recur(int level, int param) {
28

9+
// terminator
10+
if (level > MAX_LEVEL) {
11+
// process result
12+
return;
13+
}
14+
15+
// process current logic
16+
process(level, param);
17+
18+
// drill down
19+
recur( level: level + 1, newParam);
20+
21+
// restore current status
22+
23+
}
24+
25+
26+
HashMap源码分析
27+
主要阅读了hashmap中put get 方法的实现方式
328

429

0 commit comments

Comments
 (0)