From 633a7d28786a2b4b8991d015747d86a456eedd4f Mon Sep 17 00:00:00 2001 From: zouyingjie Date: Sat, 4 May 2019 16:54:20 +0800 Subject: [PATCH 1/4] =?UTF-8?q?001=20=E7=AC=AC=E4=B8=89=E5=91=A8=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_03/id_1/LeetCode_429_2.java | 43 ++++++++++++++++++++++++++++ Week_03/id_1/Leetcode_200_1.java | 48 ++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 Week_03/id_1/LeetCode_429_2.java create mode 100644 Week_03/id_1/Leetcode_200_1.java diff --git a/Week_03/id_1/LeetCode_429_2.java b/Week_03/id_1/LeetCode_429_2.java new file mode 100644 index 00000000..a5901348 --- /dev/null +++ b/Week_03/id_1/LeetCode_429_2.java @@ -0,0 +1,43 @@ +class Solution { +private List> result = new ArrayList<>(); + + public List> levelOrder(Node root) { + + if (root == null) { + return result; + } + + List nodeVals = new ArrayList<>(); + nodeVals.add(root.val); + result.add(nodeVals); + this.bfs(root.children); + return this.result; + + } + + public void bfs(List nodes) { + int length = nodes.size(); + if (length == 0) { + return; + } + + List childrens = new ArrayList<>(); + List nodeVals = new ArrayList<>(); + + for (int i = 0; i < length; i ++) { + Node node = nodes.get(i); + nodeVals.add(node.val); + + if (node.children != null) { + Iterator iterator = node.children.iterator(); + while (iterator.hasNext()) { + childrens.add(iterator.next()); + } + } + + } + + this.result.add(nodeVals); + this.bfs(childrens); + } +} \ No newline at end of file diff --git a/Week_03/id_1/Leetcode_200_1.java b/Week_03/id_1/Leetcode_200_1.java new file mode 100644 index 00000000..64402222 --- /dev/null +++ b/Week_03/id_1/Leetcode_200_1.java @@ -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 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); + } + } +} \ No newline at end of file From 519fba855afaf650b9e8b5ec0a9a977591badb91 Mon Sep 17 00:00:00 2001 From: zouyingjie Date: Sun, 5 May 2019 09:31:04 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E5=91=A8=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_03/id_1/{LeetCode_429_2.java => LeetCode_429_1.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Week_03/id_1/{LeetCode_429_2.java => LeetCode_429_1.java} (100%) diff --git a/Week_03/id_1/LeetCode_429_2.java b/Week_03/id_1/LeetCode_429_1.java similarity index 100% rename from Week_03/id_1/LeetCode_429_2.java rename to Week_03/id_1/LeetCode_429_1.java From 5b549f6ee5cb45bc483c8054eec9af27538ed34a Mon Sep 17 00:00:00 2001 From: zouyingjie Date: Sun, 12 May 2019 17:05:38 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E5=91=A8=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_04/id_1/LeetCode_746_1.java | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Week_04/id_1/LeetCode_746_1.java diff --git a/Week_04/id_1/LeetCode_746_1.java b/Week_04/id_1/LeetCode_746_1.java new file mode 100644 index 00000000..dcdc7a54 --- /dev/null +++ b/Week_04/id_1/LeetCode_746_1.java @@ -0,0 +1,33 @@ + +// 解法正确但是因为大量重复计算超时 +// 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]); + } + } \ No newline at end of file From 260ed547f9dd46d287d09e444897a9544c8ad348 Mon Sep 17 00:00:00 2001 From: zouyingjie Date: Sun, 12 May 2019 17:48:23 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E5=91=A8=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_04/id_1/LeetCode_169_1.java | 20 ++++++++++++++++++++ Week_04/id_1/LeetCode_746_1.java | 5 ++++- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 Week_04/id_1/LeetCode_169_1.java diff --git a/Week_04/id_1/LeetCode_169_1.java b/Week_04/id_1/LeetCode_169_1.java new file mode 100644 index 00000000..e618e928 --- /dev/null +++ b/Week_04/id_1/LeetCode_169_1.java @@ -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 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; + } +} \ No newline at end of file diff --git a/Week_04/id_1/LeetCode_746_1.java b/Week_04/id_1/LeetCode_746_1.java index dcdc7a54..aa5e61d3 100644 --- a/Week_04/id_1/LeetCode_746_1.java +++ b/Week_04/id_1/LeetCode_746_1.java @@ -30,4 +30,7 @@ public int minCostClimbingStairs(int[] cost) { } return Math.min(cost[cost.length-1], cost[cost.length-2]); } - } \ No newline at end of file + } + + + \ No newline at end of file