Skip to content

Commit c64291b

Browse files
committed
Week02作业补交
1 parent b9f98a2 commit c64291b

9 files changed

Lines changed: 264 additions & 0 deletions

Week02/AnagramsGroup.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package week02;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.HashMap;
6+
import java.util.List;
7+
8+
/**
9+
* 字母异位词分组
10+
*/
11+
public class AnagramsGroup {
12+
13+
public static void main(String[] args) {
14+
List<List<String>> result = groupAnagrams(new String[] {"ab", "ba"});
15+
System.out.println(result);
16+
}
17+
18+
public static List<List<String>> groupAnagrams(String[] strs) {
19+
HashMap<String, List<String>> hash = new HashMap<>();
20+
for (String str : strs) {
21+
char[] s_arr = str.toCharArray();
22+
Arrays.sort(s_arr);
23+
String key = String.valueOf(s_arr);
24+
if (hash.containsKey(key)) {
25+
hash.get(key).add(str);
26+
} else {
27+
List<String> temp = new ArrayList<>();
28+
temp.add(str);
29+
hash.put(key, temp);
30+
}
31+
32+
}
33+
return new ArrayList<>(hash.values());
34+
}
35+
36+
}

Week02/BinaryTreeInIteration.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package week02;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.ArrayList;
5+
import java.util.Deque;
6+
import java.util.List;
7+
8+
/**
9+
* 二叉树的中序遍历
10+
*/
11+
public class BinaryTreeInIteration {
12+
13+
public List<Integer> inOrderIteration(TreeNode root) {
14+
List <Integer> res = new ArrayList<>();
15+
Deque <TreeNode> stack = new ArrayDeque<>();
16+
TreeNode p = root;
17+
18+
while (!stack.isEmpty() || p != null) {
19+
while (p != null) {
20+
stack.push(p);
21+
p = p.left;
22+
}
23+
TreeNode tmp = stack.pop();
24+
res.add(tmp.val);
25+
p = tmp.right;
26+
}
27+
return res;
28+
}
29+
30+
}

Week02/BinaryTreePreIteration.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package week02;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* 二叉树的前序遍历
7+
*/
8+
public class BinaryTreePreIteration {
9+
10+
public static void preOrderIteration(TreeNode head) {
11+
if (head == null) {
12+
return;
13+
}
14+
15+
Stack<TreeNode> stack = new Stack<>();
16+
stack.push(head);
17+
18+
while (!stack.isEmpty()) {
19+
TreeNode node = stack.pop();
20+
System.out.print(node.val + " ");
21+
if (node.right != null) {
22+
stack.push(node.right);
23+
}
24+
if (node.left != null) {
25+
stack.push(node.left);
26+
}
27+
}
28+
}
29+
30+
}

Week02/IsAnagram.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package week02;
2+
3+
/**
4+
* 有效的字母异位词
5+
*/
6+
public class IsAnagram {
7+
8+
public static void main(String[] args) {
9+
boolean anagram = isAnagram("ab", "baa");
10+
System.out.println(anagram);
11+
}
12+
13+
public static boolean isAnagram(String s, String t) {
14+
int[] alphabet = new int[26];
15+
for (int i = 0; i < s.length(); i++) alphabet[s.charAt(i) - 'a']++;
16+
for (int i = 0; i < t.length(); i++) alphabet[t.charAt(i) - 'a']--;
17+
for (int i : alphabet) if (i != 0) return false;
18+
return true;
19+
}
20+
21+
}

Week02/NTreeLevelIteration.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package week02;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
import java.util.Queue;
6+
7+
/**
8+
* N叉树的层序遍历
9+
*/
10+
public class NTreeLevelIteration {
11+
12+
public List<List<Integer>> levelOrder(TreeNode root) {
13+
List<List<Integer>> ret = new LinkedList<>();
14+
15+
if (root == null) return ret;
16+
17+
Queue<TreeNode> queue = new LinkedList<>();
18+
19+
queue.offer(root);
20+
21+
while (!queue.isEmpty()) {
22+
List<Integer> curLevel = new LinkedList<>();
23+
int len = queue.size();
24+
for (int i = 0; i < len; i++) {
25+
TreeNode curr = queue.poll();
26+
curLevel.add(curr.val);
27+
for (TreeNode c : curr.children)
28+
queue.offer(c);
29+
}
30+
ret.add(curLevel);
31+
}
32+
33+
return ret;
34+
}
35+
36+
}

Week02/NTreePreIteration.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package week02;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* N叉树的前序遍历
8+
*/
9+
public class NTreePreIteration {
10+
11+
public static List<Integer> list = new ArrayList<>();
12+
13+
public static List<Integer> preorder(TreeNode root) {
14+
if (root == null) {
15+
return list;
16+
}
17+
18+
list.add(root.val);
19+
for(TreeNode node : root.children) {
20+
preorder(node);
21+
}
22+
23+
return list;
24+
}
25+
26+
}

Week02/TopKFrequent.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package week02;
2+
3+
import java.util.*;
4+
5+
/**
6+
* 前K个高频元素
7+
*/
8+
public class TopKFrequent {
9+
10+
public static void main(String[] args) {
11+
int[] nums = new int[]{1,1,1,2,2,2,3,3,5,6,7,8,8,9};
12+
int k = 2;
13+
List<Integer> result = topKFrequent(nums, k);
14+
System.out.println(result);
15+
}
16+
17+
public static List<Integer> topKFrequent(int[] nums, int k) {
18+
List<Integer> res = new ArrayList<>();
19+
HashMap<Integer,Integer> map = new HashMap<>();
20+
for(int num : nums){
21+
if (map.containsKey(num)) {
22+
map.put(num, map.get(num) + 1);
23+
} else {
24+
map.put(num, 1);
25+
}
26+
}
27+
28+
List<Integer>[] list = new List[nums.length+1];
29+
for(int key : map.keySet()){
30+
int i = map.get(key);
31+
if(list[i] == null){
32+
list[i] = new ArrayList<>();
33+
}
34+
list[i].add(key);
35+
}
36+
37+
for(int i = list.length - 1;i >= 0 && res.size() < k;i--){
38+
if(list[i] == null) continue;
39+
res.addAll(list[i]);
40+
}
41+
return res;
42+
}
43+
44+
}

Week02/TreeNode.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package week02;
2+
3+
import java.util.List;
4+
5+
public class TreeNode {
6+
7+
int val;
8+
TreeNode left;
9+
TreeNode right;
10+
List<TreeNode> children;
11+
12+
TreeNode(int x) {
13+
val = x;
14+
}
15+
}

Week02/UglyNumber.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package week02;
2+
3+
/**
4+
* 丑数
5+
*/
6+
public class UglyNumber {
7+
8+
public static void main(String[] args) {
9+
System.out.println(getUglyNumberByN(8));
10+
}
11+
12+
public static int getUglyNumberByN(int n) {
13+
int a = 0, b = 0, c = 0;
14+
int[] dp = new int[n];
15+
dp[0] = 1;
16+
for(int i = 1; i < n; i++) {
17+
int n2 = dp[a] * 2, n3 = dp[b] * 3, n5 = dp[c] * 5;
18+
dp[i] = Math.min(Math.min(n2, n3), n5);
19+
if(dp[i] == n2) a++;
20+
if(dp[i] == n3) b++;
21+
if(dp[i] == n5) c++;
22+
}
23+
return dp[n - 1];
24+
}
25+
26+
}

0 commit comments

Comments
 (0)