Skip to content
Merged
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
43 changes: 43 additions & 0 deletions Week_03/id_1/LeetCode_429_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Solution {
private List<List<Integer>> result = new ArrayList<>();

public List<List<Integer>> levelOrder(Node root) {

if (root == null) {
return result;
}

List<Integer> nodeVals = new ArrayList<>();
nodeVals.add(root.val);
result.add(nodeVals);
this.bfs(root.children);
return this.result;

}

public void bfs(List<Node> nodes) {
int length = nodes.size();
if (length == 0) {
return;
}

List<Node> childrens = new ArrayList<>();
List<Integer> nodeVals = new ArrayList<>();

for (int i = 0; i < length; i ++) {
Node node = nodes.get(i);
nodeVals.add(node.val);

if (node.children != null) {
Iterator<Node> iterator = node.children.iterator();
while (iterator.hasNext()) {
childrens.add(iterator.next());
}
}

}

this.result.add(nodeVals);
this.bfs(childrens);
}
}
48 changes: 48 additions & 0 deletions Week_03/id_1/Leetcode_200_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
public class Solution {

private int result = 0;
private char[][] grid = null;
private int row = 0;
private int column = 0;

public int numIslands(char[][] grid) {

ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
map.size();
map.put("a", "b");
map.get("a");
this.grid = grid;
this.row = grid.length;
if (row == 0) {
return row;
}
this.column = grid[0].length;

for (int i = 0; i < row; i ++) {
for (int j = 0; j < column; j ++) {
if (grid[i][j] == '1') {
result ++;
dfs(i, j);
}else {
continue;
}

}
}
return result;
}

private void dfs(int i, int j) {

if (i >= row || j >= column | i < 0 | j < 0) {
return;
}
if (grid[i][j] == '1') {
grid[i][j] = '0';
dfs(i +1, j);
dfs(i -1, j);
dfs(i , j+1);
dfs(i , j-1);
}
}
}
20 changes: 20 additions & 0 deletions Week_04/id_1/LeetCode_169_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeMap;
class Solution {

public int majorityElement(int[] nums) {
HashMap<Integer, Integer> map = new HashMap<>();

int length = nums.length;
for (int i = 0; i < length; i ++) {
int times = map.getOrDefault(nums[i], 0) + 1;
if (times > length/2) {
return nums[i];
}
map.put(nums[i], times);
}
return 0;
}
}
36 changes: 36 additions & 0 deletions Week_04/id_1/LeetCode_746_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

// 解法正确但是因为大量重复计算超时
// class Solution {
// private int[] cost;
// public int minCostClimbingStairs(int[] cost) {

// this.cost = cost;
// int len = cost.length;

// return Math.min(minCost(len-1), minCost(len-2));
// }

// public int minCost(int n) {

// if (n <= 1) {
// return this.cost[n];
// }
// return this.cost[n] + Math.min(minCost(n-1), minCost(n-2));

// }
// }

// 参考 discuss 部分的结果,将数据中的值相加,而不是取值计算
class Solution {
private int[] cost;
public int minCostClimbingStairs(int[] cost) {

for (int i = 2; i < cost.length; i++) {
cost[i] += Math.min(cost[i-1], cost[i-2]);
}
return Math.min(cost[cost.length-1], cost[cost.length-2]);
}
}