From fb521304650ce2c357dea908a0424028d5acf0e7 Mon Sep 17 00:00:00 2001 From: jianbo liu Date: Tue, 11 Jun 2019 23:51:05 +0800 Subject: [PATCH 01/10] update readme --- Week_01/id_38/README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Week_01/id_38/README.md b/Week_01/id_38/README.md index b11159e4..5febe5b3 100644 --- a/Week_01/id_38/README.md +++ b/Week_01/id_38/README.md @@ -3,7 +3,7 @@ ## 第一周题目 --- ### 数组、链表 -【简单】[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/) @@ -63,3 +63,9 @@ [236. 二叉树的最近公共祖先【中等】](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/) +> * [ ] 熟练掌握算法编码基础 +> * [ ] 掌握所有基础数据结构和算法的理论 +> * [ ] 使用 Leetcode 进行系统性练习 +> * [ ] 2019年完成 Leetcode 100题 +> * [ ] 系统性的总结、整理、分享自己的数据结构和算法学习过程 + From 5a9ec6716c0def1922ba3c578df8ea7b34e61538 Mon Sep 17 00:00:00 2001 From: jianbo liu Date: Tue, 11 Jun 2019 23:52:53 +0800 Subject: [PATCH 02/10] update readme --- Week_01/id_38/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Week_01/id_38/README.md b/Week_01/id_38/README.md index 5febe5b3..9839574e 100644 --- a/Week_01/id_38/README.md +++ b/Week_01/id_38/README.md @@ -69,3 +69,4 @@ > * [ ] 2019年完成 Leetcode 100题 > * [ ] 系统性的总结、整理、分享自己的数据结构和算法学习过程 +* [x] 系统性的总结、整理、分享自己的数据结构和算法学习过程 \ No newline at end of file From 864217e2099b68fafe49f3a11da52b7d248a145e Mon Sep 17 00:00:00 2001 From: jianbo liu Date: Wed, 12 Jun 2019 00:18:11 +0800 Subject: [PATCH 03/10] update readme --- Week_02/id_38/README.md | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Week_02/id_38/README.md diff --git a/Week_02/id_38/README.md b/Week_02/id_38/README.md new file mode 100644 index 00000000..b681e35f --- /dev/null +++ b/Week_02/id_38/README.md @@ -0,0 +1,48 @@ +# 学习笔记 + +## 第二周题目 +--- +### 哈希表 +*[ ]【简单】[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 From cd77c1e7c2636e12e1ec75a3021f93f491dd8e1b Mon Sep 17 00:00:00 2001 From: jianbo liu Date: Wed, 12 Jun 2019 00:19:50 +0800 Subject: [PATCH 04/10] update readme --- Week_02/id_38/README.md | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/Week_02/id_38/README.md b/Week_02/id_38/README.md index b681e35f..cb0fef5a 100644 --- a/Week_02/id_38/README.md +++ b/Week_02/id_38/README.md @@ -3,46 +3,46 @@ ## 第二周题目 --- ### 哈希表 -*[ ]【简单】[1. 两数之和 ](https://leetcode-cn.com/problems/two-sum/) +* [ ]【简单】[1. 两数之和 ](https://leetcode-cn.com/problems/two-sum/) -*[ ]【简单】[242. 有效的字母异位词](https://leetcode-cn.com/problems/valid-anagram/) +* [ ]【简单】[242. 有效的字母异位词](https://leetcode-cn.com/problems/valid-anagram/) -*[ ]【中等】[3. 无重复字符的最长子串](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters) +* [ ]【中等】[3. 无重复字符的最长子串](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters) -*[ ]【中等】[692. 前K个高频单词](https://leetcode-cn.com/problems/top-k-frequent-words) +* [ ]【中等】[692. 前K个高频单词](https://leetcode-cn.com/problems/top-k-frequent-words) -*[ ]【困难】[726. 原子的数量](https://leetcode-cn.com/problems/number-of-atoms/) +* [ ]【困难】[726. 原子的数量](https://leetcode-cn.com/problems/number-of-atoms/) --- ### 二叉树 -*[ ] [111. 二叉树的最小深度【简单】](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/) +* [ ] [111. 二叉树的最小深度【简单】](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/) -*[ ] [236. 二叉树的最近公共祖先【中等】](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/) +* [ ] [236. 二叉树的最近公共祖先【中等】](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/) -*[ ] [101. 对称二叉树【简单】](https://leetcode-cn.com/problems/symmetric-tree/) +* [ ] [101. 对称二叉树【简单】](https://leetcode-cn.com/problems/symmetric-tree/) -*[ ] [103. 二叉树的锯齿形层次遍历【中等】](https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/) +* [ ] [103. 二叉树的锯齿形层次遍历【中等】](https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/) -*[ ] [102. 二叉树的层次遍历【中等】](https://leetcode-cn.com/problems/binary-tree-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/) +* [ ] [297. 二叉树的序列化与反序列化【困难】](https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree/) --- ### 二叉搜索树 -*[ ] [783. 二叉搜索树结点最小距离【简单】](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/) +* [ ] [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/) +* [ ] [235. 二叉搜索树的最近公共祖先【简单】](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/) -*[ ] [98. 验证二叉搜索树【中等】](https://leetcode-cn.com/problems/validate-binary-search-tree/) +* [ ] [98. 验证二叉搜索树【中等】](https://leetcode-cn.com/problems/validate-binary-search-tree/) -*[ ] [938. 二叉搜索树的范围和【中等】](https://leetcode-cn.com/problems/range-sum-of-bst/) +* [ ] [938. 二叉搜索树的范围和【中等】](https://leetcode-cn.com/problems/range-sum-of-bst/) -*[ ] [220. 存在重复元素 III【中等】](https://leetcode-cn.com/problems/contains-duplicate-iii/) +* [ ] [220. 存在重复元素 III【中等】](https://leetcode-cn.com/problems/contains-duplicate-iii/) -*[ ] [315. 计算右侧小于当前元素的个数【困难】](https://leetcode-cn.com/problems/count-of-smaller-numbers-after-self/) +* [ ] [315. 计算右侧小于当前元素的个数【困难】](https://leetcode-cn.com/problems/count-of-smaller-numbers-after-self/) --- \ No newline at end of file From c650663e745564f717eb4e91a795a2ff4fd02be0 Mon Sep 17 00:00:00 2001 From: jianbo liu Date: Wed, 12 Jun 2019 00:20:32 +0800 Subject: [PATCH 05/10] update readme --- Week_02/id_38/README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Week_02/id_38/README.md b/Week_02/id_38/README.md index cb0fef5a..078f199d 100644 --- a/Week_02/id_38/README.md +++ b/Week_02/id_38/README.md @@ -3,15 +3,15 @@ ## 第二周题目 --- ### 哈希表 -* [ ]【简单】[1. 两数之和 ](https://leetcode-cn.com/problems/two-sum/) +* [ ] 【简单】[1. 两数之和 ](https://leetcode-cn.com/problems/two-sum/) -* [ ]【简单】[242. 有效的字母异位词](https://leetcode-cn.com/problems/valid-anagram/) +* [ ] 【简单】[242. 有效的字母异位词](https://leetcode-cn.com/problems/valid-anagram/) -* [ ]【中等】[3. 无重复字符的最长子串](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters) +* [ ] 【中等】[3. 无重复字符的最长子串](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters) -* [ ]【中等】[692. 前K个高频单词](https://leetcode-cn.com/problems/top-k-frequent-words) +* [ ] 【中等】[692. 前K个高频单词](https://leetcode-cn.com/problems/top-k-frequent-words) -* [ ]【困难】[726. 原子的数量](https://leetcode-cn.com/problems/number-of-atoms/) +* [ ] 【困难】[726. 原子的数量](https://leetcode-cn.com/problems/number-of-atoms/) --- From dca8d964af48562ccc63f52991ea7b9608174499 Mon Sep 17 00:00:00 2001 From: jianbo liu Date: Wed, 12 Jun 2019 22:27:27 +0800 Subject: [PATCH 06/10] update week01 readme --- Week_01/id_38/README.md | 52 +++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/Week_01/id_38/README.md b/Week_01/id_38/README.md index 9839574e..69b0ff78 100644 --- a/Week_01/id_38/README.md +++ b/Week_01/id_38/README.md @@ -3,70 +3,62 @@ ## 第一周题目 --- ### 数组、链表 -*[x]【简单】[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/) +* [ ] [938. 二叉搜索树的范围和【简单】](https://leetcode-cn.com/problems/range-sum-of-bst/) -[236. 二叉树的最近公共祖先【中等】](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/) - -> * [ ] 熟练掌握算法编码基础 -> * [ ] 掌握所有基础数据结构和算法的理论 -> * [ ] 使用 Leetcode 进行系统性练习 -> * [ ] 2019年完成 Leetcode 100题 -> * [ ] 系统性的总结、整理、分享自己的数据结构和算法学习过程 - -* [x] 系统性的总结、整理、分享自己的数据结构和算法学习过程 \ No newline at end of file +* [ ] [236. 二叉树的最近公共祖先【中等】](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/) From 7d3c9bce15f063c10f2ed10573deba590ab1bc00 Mon Sep 17 00:00:00 2001 From: jianbo liu Date: Thu, 13 Jun 2019 00:15:24 +0800 Subject: [PATCH 07/10] week01 homework --- Week_01/id_38/LeetCode_104_038.java | 40 +++++++++++++++++ Week_02/id_38/LeetCode_01_038.java | 66 +++++++++++++++++++++++++++++ Week_02/id_38/README.md | 2 +- 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 Week_01/id_38/LeetCode_104_038.java create mode 100644 Week_02/id_38/LeetCode_01_038.java 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_02/id_38/LeetCode_01_038.java b/Week_02/id_38/LeetCode_01_038.java new file mode 100644 index 00000000..4dc83118 --- /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) { + // 暴力,时间 O(n^2),空间 O(1) + int[] result = new int[2]; + for (int i = 0; i < nums.length - 1; ++i) { + for (int j = i + 1; j < nums.length; ++j) { + if (nums[i] + nums[j] == target) { + result[0] = i; + result[1] = j; + } + } + } + + return result; + } + + public static void main(String[] args) { + LeetCode_01_038 testCode_01_038 = new LeetCode_01_038(); + int[] nums = new int[]{-1,0,1,2,-1,-4}; + int[] result = testCode_01_038.twoSum(nums, 3); + for (int i = 0; i < result.length; ++i) { + System.out.print(result[i] + ","); + } + } +} + diff --git a/Week_02/id_38/README.md b/Week_02/id_38/README.md index 078f199d..7f497f14 100644 --- a/Week_02/id_38/README.md +++ b/Week_02/id_38/README.md @@ -3,7 +3,7 @@ ## 第二周题目 --- ### 哈希表 -* [ ] 【简单】[1. 两数之和 ](https://leetcode-cn.com/problems/two-sum/) +* [x] 【简单】[1. 两数之和 ](https://leetcode-cn.com/problems/two-sum/) * [ ] 【简单】[242. 有效的字母异位词](https://leetcode-cn.com/problems/valid-anagram/) From 0b8d63c6ccaebc5d9ca95d2571ba3003e259dc4e Mon Sep 17 00:00:00 2001 From: jianbo liu Date: Thu, 13 Jun 2019 00:17:23 +0800 Subject: [PATCH 08/10] week01 homework --- Week_02/id_38/LeetCode_01_038.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Week_02/id_38/LeetCode_01_038.java b/Week_02/id_38/LeetCode_01_038.java index 4dc83118..5d2bc33f 100644 --- a/Week_02/id_38/LeetCode_01_038.java +++ b/Week_02/id_38/LeetCode_01_038.java @@ -41,23 +41,21 @@ public int[] twoSum1(int[] nums, int target) { public int[] twoSum2(int[] nums, int target) { // 暴力,时间 O(n^2),空间 O(1) - int[] result = new int[2]; for (int i = 0; i < nums.length - 1; ++i) { for (int j = i + 1; j < nums.length; ++j) { if (nums[i] + nums[j] == target) { - result[0] = i; - result[1] = j; + return new int[]{i, j}; } } } - return result; + 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,0,1,2,-1,-4}; - int[] result = testCode_01_038.twoSum(nums, 3); + int[] result = testCode_01_038.twoSum2(nums, 3); for (int i = 0; i < result.length; ++i) { System.out.print(result[i] + ","); } From 501797c9f2740155c290072be7d29979a215bf0b Mon Sep 17 00:00:00 2001 From: jianbo liu Date: Sun, 16 Jun 2019 20:47:42 +0800 Subject: [PATCH 09/10] week2 --- Week_02/id_38/LeetCode_01_038.java | 4 +- Week_02/id_38/LeetCode_111_038.java | 26 +++++++++ Week_02/id_38/LeetCode_242_038.java | 81 +++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 Week_02/id_38/LeetCode_111_038.java create mode 100644 Week_02/id_38/LeetCode_242_038.java diff --git a/Week_02/id_38/LeetCode_01_038.java b/Week_02/id_38/LeetCode_01_038.java index 5d2bc33f..dca4e70d 100644 --- a/Week_02/id_38/LeetCode_01_038.java +++ b/Week_02/id_38/LeetCode_01_038.java @@ -40,6 +40,8 @@ public int[] twoSum1(int[] nums, int target) { } 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) { @@ -54,7 +56,7 @@ public int[] twoSum2(int[] nums, int target) { public static void main(String[] args) { LeetCode_01_038 testCode_01_038 = new LeetCode_01_038(); - int[] nums = new int[]{-1,0,1,2,-1,-4}; + 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")); + } +} + From fdee9377417f9848bd49935d6decece8cbab0cac Mon Sep 17 00:00:00 2001 From: jianbo liu Date: Sun, 23 Jun 2019 22:20:00 +0800 Subject: [PATCH 10/10] week03 --- Week_03/id_38/LeetCode_104_038.java | 29 ++++++++++++++++ Week_03/id_38/LeetCode_111_038.java | 25 ++++++++++++++ Week_03/id_38/LeetCode_200_038.java | 51 +++++++++++++++++++++++++++++ Week_03/id_38/LeetCode_703_038.java | 40 ++++++++++++++++++++++ Week_03/id_38/README.md | 29 ++++++++++++++++ 5 files changed, 174 insertions(+) create mode 100644 Week_03/id_38/LeetCode_104_038.java create mode 100644 Week_03/id_38/LeetCode_111_038.java create mode 100644 Week_03/id_38/LeetCode_200_038.java create mode 100644 Week_03/id_38/LeetCode_703_038.java create mode 100644 Week_03/id_38/README.md 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/)