Skip to content

Week03 和 Week04 作业 #825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Week_03/id_81/LeetCode_104_81.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
59 changes: 59 additions & 0 deletions Week_03/id_81/LeetCode_429_81.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @author apple
* N ✘ 树的层序遍历
*
*/
public class FloorByFloor {

// 遍历想到了用队列,先进先出,但是效率不好,应该是我用的方式不对
public static List<List<Integer>> levelOrder(Node root) {
List<List<Integer>> result = new ArrayList<>();
Queue<Node> queue = new LinkedBlockingQueue<>();
if (root == null)
return result;
queue.add(root);
while (!queue.isEmpty()) {
List<Integer> 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<Node> 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<Node> 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));
}
}

39 changes: 39 additions & 0 deletions Week_03/id_81/LeetCode_997_81.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> output = new HashMap<>();
Map<Integer, Integer> 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<Integer, Integer> 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));
}
}
34 changes: 34 additions & 0 deletions Week_04/id_81/LeetCode_169_081.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> 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<Integer, Integer> 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 }));
}
}

33 changes: 33 additions & 0 deletions Week_04/id_81/LeetCode_720_81.java
Original file line number Diff line number Diff line change
@@ -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<String> 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" });
}
}