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
40 changes: 40 additions & 0 deletions Week_01/id_38/LeetCode_104_038.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
45 changes: 22 additions & 23 deletions Week_01/id_38/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,62 @@
## 第一周题目
---
### 数组、链表
<font color="green">【简单】</font>[26. 删除排序数组中的重复项 ](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/)
* [x] <font color="green">【简单】</font>[26. 删除排序数组中的重复项 ](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/)

<font color=green>【简单】</font>[189. 旋转数组](https://leetcode-cn.com/problems/rotate-array/)
* [ ] <font color=green>【简单】</font>[189. 旋转数组](https://leetcode-cn.com/problems/rotate-array/)

<font color=green>【简单】</font>[21. 合并两个有序链表](https://leetcode-cn.com/problems/merge-two-sorted-lists/)
* [ ] <font color=green>【简单】</font>[21. 合并两个有序链表](https://leetcode-cn.com/problems/merge-two-sorted-lists/)

<font color=green>【简单】</font>[88. 合并两个有序数组](https://leetcode-cn.com/problems/merge-sorted-array/)
* [ ] <font color=green>【简单】</font>[88. 合并两个有序数组](https://leetcode-cn.com/problems/merge-sorted-array/)

<font color=orange>【中等】</font>[24. 两两交换链表中的节点](https://leetcode-cn.com/problems/swap-nodes-in-pairs/)
* [ ] <font color=orange>【中等】</font>[24. 两两交换链表中的节点](https://leetcode-cn.com/problems/swap-nodes-in-pairs/)

<font color=orange>【中等】</font>[15. 三数之和](https://leetcode-cn.com/problems/3sum/)
* [x] <font color=orange>【中等】</font>[15. 三数之和](https://leetcode-cn.com/problems/3sum/)

---

### Map & Set
[242. 有效的字母异位词<font color=green>【简单】</font>](https://leetcode-cn.com/problems/valid-anagram/)
* [x] [242. 有效的字母异位词<font color=green>【简单】</font>](https://leetcode-cn.com/problems/valid-anagram/)

[49. 字母异位词分组<font color=orange>【中等】</font>](https://leetcode-cn.com/problems/group-anagrams/)
* [ ] [49. 字母异位词分组<font color=orange>【中等】</font>](https://leetcode-cn.com/problems/group-anagrams/)

---

### 堆栈、队列

[1047. 删除字符串中的所有相邻重复项<font color=green>【简单】</font>](https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/)
* [ ] [1047. 删除字符串中的所有相邻重复项<font color=green>【简单】</font>](https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/)

[1021. 删除最外层的括号<font color=green>【简单】</font>](https://leetcode-cn.com/problems/remove-outermost-parentheses/)
* [ ] [1021. 删除最外层的括号<font color=green>【简单】</font>](https://leetcode-cn.com/problems/remove-outermost-parentheses/)

[84. 柱状图中最大的矩形<font color=red>【困难】</font>](https://leetcode.com/problems/largest-rectangle-in-histogram/)
* [ ] [84. 柱状图中最大的矩形<font color=red>【困难】</font>](https://leetcode.com/problems/largest-rectangle-in-histogram/)

[42. 接雨水<font color=red>【困难】</font>](https://leetcode.com/problems/trapping-rain-water/)
* [ ] [42. 接雨水<font color=red>【困难】</font>](https://leetcode.com/problems/trapping-rain-water/)

---

### 二分查找

[441. 排列硬币<font color=green>【简单】</font>](https://leetcode-cn.com/problems/arranging-coins/)
* [ ] [441. 排列硬币<font color=green>【简单】</font>](https://leetcode-cn.com/problems/arranging-coins/)

[50. Pow(x, n)<font color=orange>【中等】</font>](https://leetcode-cn.com/problems/powx-n/)
* [x] [50. Pow(x, n)<font color=orange>【中等】</font>](https://leetcode-cn.com/problems/powx-n/)

[174. 地下城游戏<font color=red>【困难】</font>](https://leetcode-cn.com/problems/dungeon-game/)
* [ ] [174. 地下城游戏<font color=red>【困难】</font>](https://leetcode-cn.com/problems/dungeon-game/)


---

### 递归

[104. 二叉树的最大深度<font color=green>【简单】</font>](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/)
* [ ] [104. 二叉树的最大深度<font color=green>【简单】</font>](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/)

[101. 对称二叉树<font color=green>【简单】</font>](https://leetcode-cn.com/problems/symmetric-tree/)
* [ ] [101. 对称二叉树<font color=green>【简单】</font>](https://leetcode-cn.com/problems/symmetric-tree/)

[111. 二叉树的最小深度<font color=green>【简单】</font>](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/)
* [ ] [111. 二叉树的最小深度<font color=green>【简单】</font>](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/)

[783. 二叉搜索树结点最小距离<font color=green>【简单】</font>](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/)
* [ ] [783. 二叉搜索树结点最小距离<font color=green>【简单】</font>](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/)

[257. 二叉树的所有路径<font color=green>【简单】</font>](https://leetcode-cn.com/problems/binary-tree-paths/)
* [ ] [257. 二叉树的所有路径<font color=green>【简单】</font>](https://leetcode-cn.com/problems/binary-tree-paths/)

[938. 二叉搜索树的范围和<font color=green>【简单】</font>](https://leetcode-cn.com/problems/range-sum-of-bst/)

[236. 二叉树的最近公共祖先<font color=orange>【中等】</font>](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/)
* [ ] [938. 二叉搜索树的范围和<font color=green>【简单】</font>](https://leetcode-cn.com/problems/range-sum-of-bst/)

* [ ] [236. 二叉树的最近公共祖先<font color=orange>【中等】</font>](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/)
66 changes: 66 additions & 0 deletions Week_02/id_38/LeetCode_01_038.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> numDic = new HashMap<Integer, Integer>();
// 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<Integer, Integer> numDic = new HashMap<Integer, Integer>();
// 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] + ",");
}
}
}

26 changes: 26 additions & 0 deletions Week_02/id_38/LeetCode_111_038.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

81 changes: 81 additions & 0 deletions Week_02/id_38/LeetCode_242_038.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}

48 changes: 48 additions & 0 deletions Week_02/id_38/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 学习笔记

## 第二周题目
---
### 哈希表
* [x] <font color="green">【简单】</font>[1. 两数之和 ](https://leetcode-cn.com/problems/two-sum/)

* [ ] <font color=green>【简单】</font>[242. 有效的字母异位词](https://leetcode-cn.com/problems/valid-anagram/)

* [ ] <font color=orange>【中等】</font>[3. 无重复字符的最长子串](https://leetcode-cn.com/problems/longest-substring-without-repeating-characters)

* [ ] <font color=orange>【中等】</font>[692. 前K个高频单词](https://leetcode-cn.com/problems/top-k-frequent-words)

* [ ] <font color=red>【困难】</font>[726. 原子的数量](https://leetcode-cn.com/problems/number-of-atoms/)

---

### 二叉树
* [ ] [111. 二叉树的最小深度<font color=green>【简单】</font>](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/)

* [ ] [236. 二叉树的最近公共祖先<font color=orange>【中等】</font>](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/)

* [ ] [101. 对称二叉树<font color=green>【简单】</font>](https://leetcode-cn.com/problems/symmetric-tree/)

* [ ] [103. 二叉树的锯齿形层次遍历<font color=orange>【中等】</font>](https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/)

* [ ] [102. 二叉树的层次遍历<font color=orange>【中等】</font>](https://leetcode-cn.com/problems/binary-tree-level-order-traversal/)

* [ ] [297. 二叉树的序列化与反序列化<font color=red>【困难】</font>](https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree/)


---

### 二叉搜索树
* [ ] [783. 二叉搜索树结点最小距离<font color=green>【简单】</font>](https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/)

* [ ] [235. 二叉搜索树的最近公共祖先<font color=green>【简单】</font>](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)

* [ ] [98. 验证二叉搜索树<font color=orange>【中等】</font>](https://leetcode-cn.com/problems/validate-binary-search-tree/)

* [ ] [938. 二叉搜索树的范围和<font color=orange>【中等】</font>](https://leetcode-cn.com/problems/range-sum-of-bst/)

* [ ] [220. 存在重复元素 III<font color=orange>【中等】</font>](https://leetcode-cn.com/problems/contains-duplicate-iii/)

* [ ] [315. 计算右侧小于当前元素的个数<font color=red>【困难】</font>](https://leetcode-cn.com/problems/count-of-smaller-numbers-after-self/)


---
29 changes: 29 additions & 0 deletions Week_03/id_38/LeetCode_104_038.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading