diff --git a/Week_03/id_81/LeetCode_104_81.java b/Week_03/id_81/LeetCode_104_81.java new file mode 100644 index 00000000..1f4e5c22 --- /dev/null +++ b/Week_03/id_81/LeetCode_104_81.java @@ -0,0 +1,22 @@ +/** + * 获取二叉树的最大深度 + * + * @author apple + */ +public class MaxDepth { + /** + * Definition for a binary tree node. public class TreeNode { int val; + * TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } + */ + public static int maxDepth(TreeNode root) { + return dfs(root, 0); + } + // 遇到二叉树就用递归 + public static int dfs(TreeNode root, int depth) { + if (root == null) + return 0; + if (root.left == null && root.right == null) + return 1; + return Math.max(dfs(root.left, depth) + 1, dfs(root.right, depth) + 1); + } +} diff --git a/Week_03/id_81/LeetCode_429_81.java b/Week_03/id_81/LeetCode_429_81.java new file mode 100644 index 00000000..3dab6ea4 --- /dev/null +++ b/Week_03/id_81/LeetCode_429_81.java @@ -0,0 +1,59 @@ +/** + * @author apple + * N ✘ 树的层序遍历 + * +*/ +public class FloorByFloor { + + // 遍历想到了用队列,先进先出,但是效率不好,应该是我用的方式不对 + public static List> levelOrder(Node root) { + List> result = new ArrayList<>(); + Queue queue = new LinkedBlockingQueue<>(); + if (root == null) + return result; + queue.add(root); + while (!queue.isEmpty()) { + List l = new ArrayList<>(); + int length = queue.size(); // 注意这里,我刚开始直接就用了queue. // size,导致结果不对 + for (int i = 0; i < length; ++i) { + Node node = queue.poll(); + l.add(node.val); + if (node.children != null && !node.children.isEmpty()) { + for (Node item : node.children) { + queue.add(item); + } + } + } + result.add(l); + } + return result; + } + + public static void main(String[] args) { + Node root = new Node(); + root.val = 1; + + List list = new ArrayList<>(); + Node node1 = new Node(); + node1.val = 3; + Node node2 = new Node(); + node2.val = 2; + Node node3 = new Node(); + node3.val = 4; + list.add(node1); + list.add(node2); + list.add(node3); + root.children = list; + List list1 = new ArrayList<>(); + Node node4 = new Node(); + node4.val = 5; + Node node5 = new Node(); + node5.val = 6; + list1.add(node4); + list1.add(node5); + node1.children = list1; + + System.out.println(levelOrder(root)); + } +} + diff --git a/Week_03/id_81/LeetCode_997_81.java b/Week_03/id_81/LeetCode_997_81.java new file mode 100644 index 00000000..48dfff56 --- /dev/null +++ b/Week_03/id_81/LeetCode_997_81.java @@ -0,0 +1,39 @@ +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + +/** + * 找到小镇的法官 + * + * @link leetcode 997 + * @author apple + */ +public class FindJudge { + // 目前短暂的思路: 把其想象成一个有向图,所以设置了两个map来保存,一个保 // 存其出度,一个保存其入度。如果某个的出度为0 ,入度为N-1,那就认为这个 + // 值就是要找的法官 + public static int findJudge(int N, int[][] trust) { + Map output = new HashMap<>(); + Map input = new HashMap<>(); + + for (int n = 1; n <= N; ++n) { + output.put(n, 0); + input.put(n, 0); + } + for (int i = 0; i < trust.length; ++i) { + int[] arr = trust[i]; + output.put(arr[1], output.get(arr[1]) + 1); + input.put(arr[0], input.get(arr[0]) - 1); + } + for (Entry entry : output.entrySet()) { + int key = entry.getKey(); + if (entry.getValue() == N - 1 && input.get(key) == 0) + return key; + } + return -1; + } + + public static void main(String[] args) { + int[][] trust = new int[][] { { 1, 3 }, { 2, 3 }, { 3, 1 } }; + System.out.println(findJudge(3, trust)); + } +} diff --git a/Week_04/id_81/LeetCode_169_081.java b/Week_04/id_81/LeetCode_169_081.java new file mode 100644 index 00000000..a4be6717 --- /dev/null +++ b/Week_04/id_81/LeetCode_169_081.java @@ -0,0 +1,34 @@ +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + +/** + * leetCode 169 + * + * @author apple + */ +public class Solution { + + // 利用hashMap的遍历,没有用到分分治的思想 + // 也可以使用 快排,然后取中间的数 + public static int majorityElement(int[] nums) { + Map map = new HashMap<>(); + for (int num : nums) { + if (!map.containsKey(num)) { + map.put(num, 1); + } else { + map.put(num, map.get(num) + 1); + } + } + for (Entry entry : map.entrySet()) { + if (entry.getValue() > (nums.length / 2)) + return entry.getKey(); + } + return -1; + } + + public static void main(String[] args) { + System.out.println(majorityElement(new int[] { 2, 2, 1, 1, 1, 2, 2 })); + } +} + diff --git a/Week_04/id_81/LeetCode_720_81.java b/Week_04/id_81/LeetCode_720_81.java new file mode 100644 index 00000000..c6e2fa43 --- /dev/null +++ b/Week_04/id_81/LeetCode_720_81.java @@ -0,0 +1,33 @@ +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +/** + * leetCode 720 + * + * @author apple + */ +public class Solution { + + public static String longestWord(String[] words) { + Arrays.sort(words); // 首先使用 Arrays + // 工具类对字符串数组进行排序,字符串长度长的在后面,字符串长的肯定包含字符串短的 + Set set = new HashSet<>(); // 保存满足要求的字符串 + String res = ""; + // 开始遍历数组 + for (String s : words) { + // 如果字符串的长度为1, 那么res=s,并且将其放到set中,如果字符串的长度大于1,那么如果其 length-1 在set + // 中,那么res = s + if (s.length() == 1 || set.contains(s.substring(0, s.length() - 1))) { + res = s.length() > res.length() ? s : res; + set.add(s); + } + } + return res; + } + + public static void main(String[] args) { + longestWord(new String[] { "w", "a", "wo", "wor", "worl", "world" }); + } +} +