diff --git a/Week_01/id_38/LeetCode_104_038.java b/Week_01/id_38/LeetCode_104_038.java new file mode 100644 index 00000000..13cde551 --- /dev/null +++ b/Week_01/id_38/LeetCode_104_038.java @@ -0,0 +1,40 @@ +/* + * @lc app=leetcode.cn id=104 lang=java + * + * [104] 二叉树的最大深度 + */ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +class LeetCode_104_038 { + public int maxDepth(TreeNode root) { + if (root == null) { + return 0; + } else { + int left_height = maxDepth(root.left); + int right_height = maxDepth(root.right); + return java.lang.Math.max(left_height, right_height) + 1; + } + } + + public static void main(String[] args) { + LeetCode_104_038 testCode_104_038 = new LeetCode_104_038(); + System.out.println(testCode_104_038.maxDepth(nums)); + } +} + +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } + } diff --git a/Week_01/id_38/README.md b/Week_01/id_38/README.md index b11159e4..69b0ff78 100644 --- a/Week_01/id_38/README.md +++ b/Week_01/id_38/README.md @@ -3,63 +3,62 @@ ## 第一周题目 --- ### 数组、链表 -【简单】[26. 删除排序数组中的重复项 ](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/) +* [x] 【简单】[26. 删除排序数组中的重复项 ](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/) -【简单】[189. 旋转数组](https://leetcode-cn.com/problems/rotate-array/) +* [ ] 【简单】[189. 旋转数组](https://leetcode-cn.com/problems/rotate-array/) -【简单】[21. 合并两个有序链表](https://leetcode-cn.com/problems/merge-two-sorted-lists/) +* [ ] 【简单】[21. 合并两个有序链表](https://leetcode-cn.com/problems/merge-two-sorted-lists/) -【简单】[88. 合并两个有序数组](https://leetcode-cn.com/problems/merge-sorted-array/) +* [ ] 【简单】[88. 合并两个有序数组](https://leetcode-cn.com/problems/merge-sorted-array/) -【中等】[24. 两两交换链表中的节点](https://leetcode-cn.com/problems/swap-nodes-in-pairs/) +* [ ] 【中等】[24. 两两交换链表中的节点](https://leetcode-cn.com/problems/swap-nodes-in-pairs/) -【中等】[15. 三数之和](https://leetcode-cn.com/problems/3sum/) +* [x] 【中等】[15. 三数之和](https://leetcode-cn.com/problems/3sum/) --- ### Map & Set -[242. 有效的字母异位词【简单】](https://leetcode-cn.com/problems/valid-anagram/) +* [x] [242. 有效的字母异位词【简单】](https://leetcode-cn.com/problems/valid-anagram/) -[49. 字母异位词分组【中等】](https://leetcode-cn.com/problems/group-anagrams/) +* [ ] [49. 字母异位词分组【中等】](https://leetcode-cn.com/problems/group-anagrams/) --- ### 堆栈、队列 -[1047. 删除字符串中的所有相邻重复项【简单】](https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/) +* [ ] [1047. 删除字符串中的所有相邻重复项【简单】](https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/) -[1021. 删除最外层的括号【简单】](https://leetcode-cn.com/problems/remove-outermost-parentheses/) +* [ ] [1021. 删除最外层的括号【简单】](https://leetcode-cn.com/problems/remove-outermost-parentheses/) -[84. 柱状图中最大的矩形【困难】](https://leetcode.com/problems/largest-rectangle-in-histogram/) +* [ ] [84. 柱状图中最大的矩形【困难】](https://leetcode.com/problems/largest-rectangle-in-histogram/) -[42. 接雨水【困难】](https://leetcode.com/problems/trapping-rain-water/) +* [ ] [42. 接雨水【困难】](https://leetcode.com/problems/trapping-rain-water/) --- ### 二分查找 -[441. 排列硬币【简单】](https://leetcode-cn.com/problems/arranging-coins/) +* [ ] [441. 排列硬币【简单】](https://leetcode-cn.com/problems/arranging-coins/) -[50. Pow(x, n)【中等】](https://leetcode-cn.com/problems/powx-n/) +* [x] [50. Pow(x, n)【中等】](https://leetcode-cn.com/problems/powx-n/) -[174. 地下城游戏【困难】](https://leetcode-cn.com/problems/dungeon-game/) +* [ ] [174. 地下城游戏【困难】](https://leetcode-cn.com/problems/dungeon-game/) --- ### 递归 -[104. 二叉树的最大深度【简单】](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/) +* [ ] [104. 二叉树的最大深度【简单】](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/) -[101. 对称二叉树【简单】](https://leetcode-cn.com/problems/symmetric-tree/) +* [ ] [101. 对称二叉树【简单】](https://leetcode-cn.com/problems/symmetric-tree/) -[111. 二叉树的最小深度【简单】](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/) +* [ ] [111. 二叉树的最小深度【简单】](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/) -[783. 二叉搜索树结点最小距离【简单】](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/) +* [ ] [783. 二叉搜索树结点最小距离【简单】](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/) -[257. 二叉树的所有路径【简单】](https://leetcode-cn.com/problems/binary-tree-paths/) +* [ ] [257. 二叉树的所有路径【简单】](https://leetcode-cn.com/problems/binary-tree-paths/) -[938. 二叉搜索树的范围和【简单】](https://leetcode-cn.com/problems/range-sum-of-bst/) - -[236. 二叉树的最近公共祖先【中等】](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/) +* [ ] [938. 二叉搜索树的范围和【简单】](https://leetcode-cn.com/problems/range-sum-of-bst/) +* [ ] [236. 二叉树的最近公共祖先【中等】](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/) diff --git a/Week_02/id_38/LeetCode_01_038.java b/Week_02/id_38/LeetCode_01_038.java new file mode 100644 index 00000000..dca4e70d --- /dev/null +++ b/Week_02/id_38/LeetCode_01_038.java @@ -0,0 +1,66 @@ +import java.util.*; + +/* + * @lc app=leetcode.cn id=1 lang=java + * + * [1] 两数之和 + */ +class LeetCode_01_038 { + public int[] twoSum(int[] nums, int target) { + // hashmap, 时间 O(n),空间 O(n) + Map numDic = new HashMap(); + // push and process + for (int i = 0; i < nums.length; ++i) { + if (numDic.containsKey(target - nums[i]) + && numDic.get(target - nums[i]) != i) { + return new int[]{numDic.get(target - nums[i]), i}; + } + numDic.put(nums[i], i); + } + + throw new IllegalArgumentException("No two sum solution"); + } + + public int[] twoSum1(int[] nums, int target) { + // hashmap, 时间 O(2n),空间 O(n) + Map numDic = new HashMap(); + // push + for (int i = 0; i < nums.length; ++i) { + numDic.put(nums[i], i); + } + + for (int i = 0; i < nums.length; ++i) { + if (numDic.containsKey(target - nums[i]) + && numDic.get(target - nums[i]) != i) { + return new int[]{i, numDic.get(target - nums[i])}; + } + } + + throw new IllegalArgumentException("No two sum solution"); + } + + public int[] twoSum2(int[] nums, int target) { + // check valid + + // 暴力,时间 O(n^2),空间 O(1) + for (int i = 0; i < nums.length - 1; ++i) { + for (int j = i + 1; j < nums.length; ++j) { + if (nums[i] + nums[j] == target) { + return new int[]{i, j}; + } + } + } + + throw new IllegalArgumentException("No two sum solution"); + } + + public static void main(String[] args) { + LeetCode_01_038 testCode_01_038 = new LeetCode_01_038(); + int[] nums = new int[]{-1}; + int[] result = testCode_01_038.twoSum2(nums, 3); + for (int i = 0; i < result.length; ++i) { + System.out.print(result[i] + ","); + } + } +} + diff --git a/Week_02/id_38/LeetCode_111_038.java b/Week_02/id_38/LeetCode_111_038.java new file mode 100644 index 00000000..5871e376 --- /dev/null +++ b/Week_02/id_38/LeetCode_111_038.java @@ -0,0 +1,26 @@ +import java.util.*; + +/* + * @lc app=leetcode.cn id=111 lang=java + * + * [111] 二叉树的最小深度 + */ +class LeetCode_111_038 { + public int minDepth(TreeNode root) { + if (root == null) return 0; + if ((root.l == null) && (root.r == null)) return 1; + if (root.l == null) return minDepth(root.r) + 1; + if (root.r == null) return minDepth(root.l) + 1; + return Math.min(minDepth(root.l), minDepth(root.r)) + 1; + } +} + +class TreeNode { + int val; + TreeNode l; + TreeNode r; + TreeNode(int x) { + this.val = x; + } +} + diff --git a/Week_02/id_38/LeetCode_242_038.java b/Week_02/id_38/LeetCode_242_038.java new file mode 100644 index 00000000..fdec858b --- /dev/null +++ b/Week_02/id_38/LeetCode_242_038.java @@ -0,0 +1,81 @@ +import java.util.Arrays; +import java.util.List; + +/* + * @lc app=leetcode.cn id=242 lang=java + * + * [242] 有效的字母异位词 + */ +class LeetCode_242_038 { + + /** + * hash + * 执行用时:11ms,内存消耗:38.2mb + * 时间:O(n), 空间:O(1),n为字符串长度 + * @param s + * @param t + * @return + */ + public boolean isAnagram(String s, String t) { + // check valid + if (equals(s, t)) return true; + if (length(s) != length(t)) return false; + + int[] charArr = new int[26]; + for (int i = 0; i < s.length(); ++i) { + charArr[s.charAt(i) - 'a']++; + charArr[t.charAt(i) - 'a']--; + } + + for (int i : charArr) { + if (i != 0) return false; + } + return true; + } + + /** + * 将两string排序,然后判断是否相同 + * 执行用时:317ms,内存消耗:58.2mb + * -。- + * 自行实现的算法效率正是低,代码也一大坨 + * @param s + * @param t + * @return + */ + public boolean isAnagram2(String s, String t) { + if (equals(s, t)) return true; + if (length(s) != length(t)) return false; + + return equals(stringSort(s), stringSort(t)); + } + + public String stringSort(String s) { + if (isEmpty(s)) return s; + String[] sa = s.split(""); + Arrays.sort(sa); + System.out.println(String.join("", sa)); + return String.join("", sa); + } + + public boolean isEmpty(String s) { + if (s == null || s.length() == 0) return true; + return false; + } + + public boolean equals(String s, String t) { + if (isEmpty(s) && isEmpty(t)) return true; + if (!isEmpty(s) && s.equals(t)) return true; + return false; + } + + public int length(String s) { + if (isEmpty(s)) return 0; + return s.length(); + } + + public static void main(String[] args) { + LeetCode_242_038 testCode_242_038 = new LeetCode_242_038(); + System.out.print(testCode_242_038.isAnagram("ab", "ba")); + } +} + diff --git a/Week_02/id_38/README.md b/Week_02/id_38/README.md new file mode 100644 index 00000000..7f497f14 --- /dev/null +++ b/Week_02/id_38/README.md @@ -0,0 +1,48 @@ +# 学习笔记 + +## 第二周题目 +--- +### 哈希表 +* [x] 【简单】[1. 两数之和 ](https://leetcode-cn.com/problems/two-sum/) + +* [ ] 【简单】[242. 有效的字母异位词](https://leetcode-cn.com/problems/valid-anagram/) + +* [ ] 【中等】[3. 无重复字符的最长子串](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters) + +* [ ] 【中等】[692. 前K个高频单词](https://leetcode-cn.com/problems/top-k-frequent-words) + +* [ ] 【困难】[726. 原子的数量](https://leetcode-cn.com/problems/number-of-atoms/) + +--- + +### 二叉树 +* [ ] [111. 二叉树的最小深度【简单】](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/) + +* [ ] [236. 二叉树的最近公共祖先【中等】](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/) + +* [ ] [101. 对称二叉树【简单】](https://leetcode-cn.com/problems/symmetric-tree/) + +* [ ] [103. 二叉树的锯齿形层次遍历【中等】](https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/) + +* [ ] [102. 二叉树的层次遍历【中等】](https://leetcode-cn.com/problems/binary-tree-level-order-traversal/) + +* [ ] [297. 二叉树的序列化与反序列化【困难】](https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree/) + + +--- + +### 二叉搜索树 +* [ ] [783. 二叉搜索树结点最小距离【简单】](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/) + +* [ ] [235. 二叉搜索树的最近公共祖先【简单】](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/) + +* [ ] [98. 验证二叉搜索树【中等】](https://leetcode-cn.com/problems/validate-binary-search-tree/) + +* [ ] [938. 二叉搜索树的范围和【中等】](https://leetcode-cn.com/problems/range-sum-of-bst/) + +* [ ] [220. 存在重复元素 III【中等】](https://leetcode-cn.com/problems/contains-duplicate-iii/) + +* [ ] [315. 计算右侧小于当前元素的个数【困难】](https://leetcode-cn.com/problems/count-of-smaller-numbers-after-self/) + + +--- \ No newline at end of file diff --git a/Week_03/id_38/LeetCode_104_038.java b/Week_03/id_38/LeetCode_104_038.java new file mode 100644 index 00000000..ed58b7a4 --- /dev/null +++ b/Week_03/id_38/LeetCode_104_038.java @@ -0,0 +1,29 @@ +/* + * @lc app=leetcode.cn id=104 lang=java + * + * [104] 二叉树的最大深度 + */ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +class LeetCode_104_038 { + public int maxDepth(TreeNode root) { + return root == null ? 0 : Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; + } +} + +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } + } diff --git a/Week_03/id_38/LeetCode_111_038.java b/Week_03/id_38/LeetCode_111_038.java new file mode 100644 index 00000000..67556fd9 --- /dev/null +++ b/Week_03/id_38/LeetCode_111_038.java @@ -0,0 +1,25 @@ +import java.util.*; + +/* + * @lc app=leetcode.cn id=111 lang=java + * + * [111] 二叉树的最小深度 + */ +class LeetCode_111_038 { + public int minDepth(TreeNode root) { + if (root == null) return 0; + if (root.l == null) return minDepth(root.r) + 1; + if (root.r == null) return minDepth(root.l) + 1; + return Math.min(minDepth(root.l), minDepth(root.r)) + 1; + } +} + +class TreeNode { + int val; + TreeNode l; + TreeNode r; + TreeNode(int x) { + this.val = x; + } +} + diff --git a/Week_03/id_38/LeetCode_200_038.java b/Week_03/id_38/LeetCode_200_038.java new file mode 100644 index 00000000..56c75a9c --- /dev/null +++ b/Week_03/id_38/LeetCode_200_038.java @@ -0,0 +1,51 @@ +/* + * @lc app=leetcode.cn id=200 lang=java + * + * [200] 岛屿数量 + */ +class LeetCode_200_038 { + /** + * 染色 + * @param grid + * @return + */ + public int numIslands(char[][] grid) { + // check valid + if(grid == null || grid.length == 0) return 0; + + int count = 0; + int col = grid[0].length; + int row = grid.length; + for(int i = 0; i < row; ++i) { + for(int j = 0; j < col; ++j) { + if(grid[i][j] == '1') { + count++; + bfs(i, j, grid); + } + } + } + + return count; + } + + public void bfs(int col, int row, char[][] grid) { + if(col < 0 || col >= grid.length) return; + if(row < 0 || row >= grid[0].length) return; + if(grid[col][row] == '1') { + grid[col][row] = '0'; + bfs(col + 1, row, grid); + bfs(col, row + 1, grid); + bfs(col - 1, row, grid); + bfs(col, row - 1, grid); + } + } + + public static void main(String[] args) { + LeetCode_200_038 testCode_200_038 = new LeetCode_200_038(); + char[][] grid = new char[][]{{'1', '1', '1', '1', '0'}, + {'1', '1', '0', '1', '0'}, + {'1', '1', '0', '0', '0'}, + {'0', '0', '0', '0', '0'}}; + System.out.println(testCode_200_038.numIslands(grid)); + } +} \ No newline at end of file diff --git a/Week_03/id_38/LeetCode_703_038.java b/Week_03/id_38/LeetCode_703_038.java new file mode 100644 index 00000000..64ae1677 --- /dev/null +++ b/Week_03/id_38/LeetCode_703_038.java @@ -0,0 +1,40 @@ +/* + * @lc app=leetcode.cn id=703 lang=java + * + * [703] 数据流中的第K大元素 + */ +class LeetCode_703_038 { + + public static void main(String[] args) { + KthLargest kthLargest = new KthLargest(3, new int[]{1, 2, 4, 9}); + System.out.println(kthLargest.add(3)); + System.out.println(kthLargest.add(0)); + System.out.println(kthLargest.add(8)); + } +} + +/** + * 优先队列 + */ +class KthLargest { + + final PriorityQueue pq; + final int k; + public KthLargest(int k, int[] nums) { + this.k = k; + pq = new PriorityQueue<>(k); + for(int i : nums) { + add(i); + } + } + + public int add(int val) { + if(pq.size() < k) { + pq.offer(val); + }else if(pq.peek() < val) { + pq.poll(); + pq.offer(val); + } + return pq.peek(); + } +} \ No newline at end of file diff --git a/Week_03/id_38/README.md b/Week_03/id_38/README.md new file mode 100644 index 00000000..34d9e3de --- /dev/null +++ b/Week_03/id_38/README.md @@ -0,0 +1,29 @@ +# 学习笔记 + +## 第三周题目 +--- + +### 图 +* [x] 【中等】[200. 岛屿数量](https://leetcode-cn.com/problems/number-of-islands/) +* [ ] 【中等】[210. 课程表 II](https://leetcode-cn.com/problems/course-schedule-ii) +--- +### 堆和排序 +* [x] 【简单】[703. 数据流中的第K大元素](https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/) +* [ ] 【困难】[295. 数据流的中位数](https://leetcode-cn.com/problems/find-median-from-data-stream/) +--- +### DFS +* [x] 【简单】[104. 二叉树的最大深度](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/) +* [x] 【简单】[111. 二叉树的最小深度](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/) +* [ ] 【中等】[547. 朋友圈](https://leetcode-cn.com/problems/friend-circles/) +* [ ] 【中等】[802. 找到最终的安全状态](https://leetcode-cn.com/problems/find-eventual-safe-states/) +* [ ] 【困难】[329. 矩阵中的最长递增路径](https://leetcode-cn.com/problems/longest-increasing-path-in-a-matrix/) +* [ ] 【中等】[529. 扫雷游戏](https://leetcode-cn.com/problems/minesweeper/) +* [ ] 【困难】[127. 单词接龙](https://leetcode-cn.com/problems/word-ladder/) +--- +### BFS +* [ ] 【简单】[429. N叉树的层序遍历](https://leetcode-cn.com/problems/n-ary-tree-level-order-traversal/) +* [ ] 【中等】[102. 二叉树的层次遍历](https://leetcode-cn.com/problems/binary-tree-level-order-traversal/) +* [ ] 【简单】[107. 二叉树的层次遍历 II](https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/) +* [ ] 【中等】[103. 二叉树的锯齿形层次遍历](https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/) +* [ ] 【中等】[529. 扫雷游戏](https://leetcode-cn.com/problems/minesweeper/) +* [ ] 【中等】[310. 最小高度树](https://leetcode-cn.com/problems/minimum-height-trees/)