import java.util.*; public class Class2Week2 { //两数之和 public int[] twoSum(int[] nums, int target) { HashMap map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { int key = target - nums[i]; if (map.containsKey(key)) { return new int[]{map.get(key), i}; } map.put(nums[i], i); } return null; } //242. 有效的字母异位词 public boolean isAnagram(String s, String t) { if (s.length() != t.length()) { return false; } return false; } //94. 二叉树的中序遍历 public List inorderTraversal(TreeNode root) { List res = new ArrayList<>(); Stack stack = new Stack<>(); TreeNode cur = root; while (cur != null || stack.size() > 0) { while (cur != null) { stack.push(cur); cur = cur.left; } TreeNode pop = stack.pop(); res.add(pop.val); cur = cur.right; } return res; } //144. 二叉树的前序遍历 public List preorderTraversal(TreeNode root) { List res = new ArrayList<>(); Stack stack = new Stack<>(); TreeNode cur = root; while (cur != null || stack.size() > 0) { while (cur != null) { stack.push(cur); res.add(cur.val); cur = cur.left; } TreeNode pop = stack.pop(); cur = pop.right; } return res; } //589. N叉树的前序遍历 public List preorder(Node root) { List res = new ArrayList<>(); Stack stack = new Stack<>(); Node cur = root; stack.push(root); while (stack != null) { Node pop = stack.pop(); res.add(pop.val); Collections.reverse(pop.children); for (Node child : pop.children) { stack.push(child); } } return res; } public List topKFrequent(int[] nums, int k) { HashMap hashMap = new HashMap<>(); for (int num : nums) { if (hashMap.containsKey(num)) { hashMap.put(num, hashMap.get(num) + 1); } else { hashMap.put(num, 1); } } PriorityQueue heap = new PriorityQueue((x, y) -> hashMap.get(x) - hashMap.get(y)); Set set = hashMap.keySet(); for (Integer n : set) { heap.add(n); if (heap.size() > k) { heap.poll(); } } List topk = new LinkedList(); while (!heap.isEmpty()) { topk.add(heap.poll()); } Collections.reverse(topk); return topk; } } //347. 前 K 个高频元素 class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } class Node { public int val; public List children; public Node() { } public Node(int _val) { val = _val; } public Node(int _val, List _children) { val = _val; children = _children; } }