diff --git a/Week_01/id_23/NOTE.md b/Week_01/id_23/NOTE.md index 28887d34..b4516261 100644 --- a/Week_01/id_23/NOTE.md +++ b/Week_01/id_23/NOTE.md @@ -5,7 +5,6 @@ #### 关于Big O 数据结构和算法本身解决的是“快”和“省”的问题。大O表示的是算法运行时间的增速! -极客时间版权所有: https://time.geekbang.org/column/article/40036 #### 关于递归 Recursion “如果使用循环,程序地性能可能更高;如果适用递归,程序可能更容易理解。如何选择要看什么对你来说更重要” --Leigh Caldwell 递归套路模板 1. Recursion terminator2. Process logic 3.Drill down 4.Reverse the current level status if needed diff --git a/Week_02/id_23/LeetCode_03_23.txt b/Week_02/id_23/LeetCode_03_23.txt new file mode 100644 index 00000000..619500d4 --- /dev/null +++ b/Week_02/id_23/LeetCode_03_23.txt @@ -0,0 +1,37 @@ +//给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 +// +// 示例 1: +// +// 输入: "abcabcbb" +//输出: 3 +//解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。 +// +// +// 示例 2: +// +// 输入: "bbbbb" +//输出: 1 +//解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。 +// +// +// 示例 3: +// +// 输入: "pwwkew" +//输出: 3 +//解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。 +//请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。 +// +// +public class Solution { + public int lengthOfLongestSubstring(String s) { + int n = s.length(), ans = 0; + // current index of character + int[] index = new int[128]; + for (int j = 0, i = 0; j < n; j++) { + i = Math.max(index[s.charAt(j)], i); + ans = Math.max(ans, j - i + 1); + index[s.charAt(j)] = j + 1; + } + return ans; + } +} \ No newline at end of file diff --git a/Week_02/id_23/LeetCode_111_23.txt b/Week_02/id_23/LeetCode_111_23.txt new file mode 100644 index 00000000..6bfd65ab --- /dev/null +++ b/Week_02/id_23/LeetCode_111_23.txt @@ -0,0 +1,23 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +class Solution { + public int minDepth(TreeNode root) { + if (root == null) { + return 0; + } + if (root.left == null) { + return minDepth(root.right) + 1; + } + if (root.right == null) { + return minDepth(root.left) + 1; + } + return Math.min(minDepth(root.left), minDepth(root.right)) + 1; + } +} \ No newline at end of file diff --git a/Week_02/id_23/NOTE.md b/Week_02/id_23/NOTE.md index 107ea7d6..e1c7a336 100644 --- a/Week_02/id_23/NOTE.md +++ b/Week_02/id_23/NOTE.md @@ -1 +1,6 @@ -# 学习笔记 +# 学习笔记 +整数数组作为直接访问表来替换 Map,是大神一样的操作哇。 +int [26] 用于字母 ‘a’ - ‘z’ 或 ‘A’ - ‘Z’ +int [128] 用于ASCII码 +int [256] 用于扩展ASCII码 +以后关于char的题目可以都用这种思路去降维思考。 diff --git a/Week_03/id_23/LeetCode_104.1_23 b/Week_03/id_23/LeetCode_104.1_23 new file mode 100644 index 00000000..7b1e1c94 --- /dev/null +++ b/Week_03/id_23/LeetCode_104.1_23 @@ -0,0 +1,38 @@ +//给定一个二叉树,找出其最大深度。 +// +// 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。 +// +// 说明: 叶子节点是指没有子节点的节点。 +// +// 示例: +//给定二叉树 [3,9,20,null,null,15,7], +// +// 3 +// / \ +// 9 20 +// / \ +// 15 7 +// +// 返回它的最大深度 3 。 +// + +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +class Solution { + 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; + } + } +} \ No newline at end of file diff --git a/Week_03/id_23/LeetCode_104.2_23 b/Week_03/id_23/LeetCode_104.2_23 new file mode 100644 index 00000000..e244f219 --- /dev/null +++ b/Week_03/id_23/LeetCode_104.2_23 @@ -0,0 +1,14 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +class Solution { + public int maxDepth(TreeNode root) { + return root == null ? 0 : Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; + } +} diff --git a/Week_03/id_23/NOTE.md b/Week_03/id_23/NOTE.md index 107ea7d6..eb800f80 100644 --- a/Week_03/id_23/NOTE.md +++ b/Week_03/id_23/NOTE.md @@ -1 +1,13 @@ -# 学习笔记 +# 学习笔记 +###本周总结 +#### 关于图的定义 +图的表示: 邻接表 和 邻接矩阵 +图可以分为 有向图 和无向图 +无向图是一种特殊的有向图 +有权图 和 无权图 + +#### 关于图相关的算法与问题 +图的遍历: DFS BFS 常见可以解决的问题有: 联通分量 Flood Fill 寻路 走迷宫 迷宫生成 无权图的最短路径 环的判断 +最小生成树问题(Minimum Spanning Tree) Prim Kruskal +最短路径问题(Shortest Path) Dijkstra Bellman-Ford +拓扑排序(Topological sorting)