Skip to content

Commit

Permalink
feat: 修改错别字,并添加题解 (azl397985856#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
Geekhyt committed Apr 15, 2020
1 parent 17de695 commit f04b4f9
Show file tree
Hide file tree
Showing 10 changed files with 445 additions and 6 deletions.
8 changes: 7 additions & 1 deletion README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ The data structures mainly includes:
#### Easy

- [0001.TwoSum](./problems/1.TwoSum.md)🆕
- [0020.Valid Parentheses](./problems/20.validParentheses.md)
- [0021.MergeTwoSortedLists](./problems/21.MergeTwoSortedLists.md) 🆕
- [0026.remove-duplicates-from-sorted-array](./problems/26.remove-duplicates-from-sorted-array.md)
- [0053.maximum-sum-subarray](./problems/53.maximum-sum-subarray-en.md) 🆕
- [0088.merge-sorted-array](./problems/88.merge-sorted-array.md)
Expand All @@ -136,9 +138,10 @@ The data structures mainly includes:
- [0263.ugly-number](./problems/263.ugly-number.md)
- [0283.move-zeroes](./problems/283.move-zeroes.md)
- [0342.power-of-four](./problems/342.power-of-four.md)
- [0371.sum-of-two-integers](./problems/371.sum-of-two-integers.md)
- [0349.intersection-of-two-arrays](./problems/349.intersection-of-two-arrays.md)
- [0437.path-sum-iii](./problems/437.path-sum-iii.md) 🆕
- [0371.sum-of-two-integers](./problems/371.sum-of-two-integers.md)
- [0455.AssignCookies](./problems/455.AssignCookies.md) 🆕
- [0501.find-mode-in-binary-search-tree](./problems/501.Find-Mode-in-Binary-Search-Tree.md) 🆕
- [0575.distribute-candies](./problems/575.distribute-candies.md)

Expand All @@ -149,7 +152,9 @@ The data structures mainly includes:
- [0005.longest-palindromic-substring](./problems/5.longest-palindromic-substring.md)
- [0011.container-with-most-water](./problems/11.container-with-most-water.md)
- [0015.3-sum](./problems/15.3-sum.md)
- [0017.Letter-Combinations-of-a-Phone-Number](./problems/17.Letter-Combinations-of-a-Phone-Number.md) 🆕
- [0019. Remove Nth Node From End of List](./problems/19.removeNthNodeFromEndofList.md)
- [0022.GenerateParentheses](./problems/22.GenerateParentheses.md) 🆕
- [0024. Swap Nodes In Pairs](./problems/24.swapNodesInPairs.md)
- [0029.divide-two-integers](./problems/29.divide-two-integers.md)
- [0031.next-permutation](./problems/31.next-permutation.md)
Expand Down Expand Up @@ -227,6 +232,7 @@ The data structures mainly includes:
- [0025.reverse-nodes-in-k-group](./problems/25.reverse-nodes-in-k-groups-en.md) 🆕
- [0032.longest-valid-parentheses](./problems/32.longest-valid-parentheses.md) 🆕
- [0042.trapping-rain-water](./problems/42.trapping-rain-water.md)
- [0052.N-Queens-II](./problems/52.N-Queens-II.md) 🆕
- [0124.binary-tree-maximum-path-sum](./problems/124.binary-tree-maximum-path-sum.md)
- [0128.longest-consecutive-sequence](./problems/128.longest-consecutive-sequence.md)
- [0145.binary-tree-postorder-traversal](./problems/145.binary-tree-postorder-traversal.md)
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ leetcode 题解,记录自己的 leetcode 解题之路。
#### 简单难度

- [0001.TwoSum](./problems/1.TwoSum.md) 🆕
- [0020.Valid Parentheses](./problems/20.validParentheses.md)
- [0021.MergeTwoSortedLists](./problems/21.MergeTwoSortedLists.md) 🆕
- [0026.remove-duplicates-from-sorted-array](./problems/26.remove-duplicates-from-sorted-array.md)
- [0053.maximum-sum-subarray](./problems/53.maximum-sum-subarray-cn.md) 🆕
- [0088.merge-sorted-array](./problems/88.merge-sorted-array.md)
Expand All @@ -147,8 +149,9 @@ leetcode 题解,记录自己的 leetcode 解题之路。
- [0283.move-zeroes](./problems/283.move-zeroes.md)
- [0342.power-of-four](./problems/342.power-of-four.md)
- [0349.intersection-of-two-arrays](./problems/349.intersection-of-two-arrays.md)
- [0437.path-sum-iii](./problems/437.path-sum-iii.md) 🆕
- [0371.sum-of-two-integers](./problems/371.sum-of-two-integers.md)
- [0437.path-sum-iii](./problems/437.path-sum-iii.md) 🆕
- [0455.AssignCookies](./problems/455.AssignCookies.md) 🆕
- [0501.find-mode-in-binary-search-tree](./problems/501.Find-Mode-in-Binary-Search-Tree.md)🆕
- [0575.distribute-candies](./problems/575.distribute-candies.md)
- [0874.walking-robot-simulation](./problems/874.walking-robot-simulation.md) 🆕
Expand All @@ -162,7 +165,9 @@ leetcode 题解,记录自己的 leetcode 解题之路。
- [0005.longest-palindromic-substring](./problems/5.longest-palindromic-substring.md)
- [0011.container-with-most-water](./problems/11.container-with-most-water.md)
- [0015.3-sum](./problems/15.3-sum.md)
- [0017.Letter-Combinations-of-a-Phone-Number](./problems/17.Letter-Combinations-of-a-Phone-Number.md) 🆕
- [0019. Remove Nth Node From End of List](./problems/19.removeNthNodeFromEndofList.md)
- [0022.GenerateParentheses](./problems/22.GenerateParentheses.md) 🆕
- [0024. Swap Nodes In Pairs](./problems/24.swapNodesInPairs.md)
- [0029.divide-two-integers](./problems/29.divide-two-integers.md)
- [0031.next-permutation](./problems/31.next-permutation.md)
Expand Down Expand Up @@ -265,6 +270,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
- [0030.substring-with-concatenation-of-all-words](./problems/30.substring-with-concatenation-of-all-words.md)
- [0032.longest-valid-parentheses](./problems/32.longest-valid-parentheses.md)
- [0042.trapping-rain-water](./problems/42.trapping-rain-water.md)
- [0052.N-Queens-II](./problems/52.N-Queens-II.md) 🆕
- [0084.largest-rectangle-in-histogram](./problems/84.largest-rectangle-in-histogram.md) 🆕
- [0085.maximal-rectangle](./problems/85.maximal-rectangle.md)
- [0124.binary-tree-maximum-path-sum](./problems/124.binary-tree-maximum-path-sum.md)
Expand Down
50 changes: 50 additions & 0 deletions problems/1.TwoSum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
## 题目地址
https://leetcode-cn.com/problems/two-sum

## 题目描述
```
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
```
## 思路
最容易想到的就是暴力枚举,我们可以利用两层 for 循环来遍历每个元素,并查找满足条件的目标元素。不过这样时间复杂度为 O(N^2),空间复杂度为 O(1),时间复杂度较高,我们要想办法进行优化。我们可以增加一个 Map 记录已经遍历过的数字及其对应的索引值。这样当遍历一个新数字的时候去 Map 里查询,target 与该数的差值是否已经在前面的数字中出现过。如果出现过,那么已经得出答案,就不必再往下执行了。

## 关键点

- 求和转换为求差
- 借助 Map 结构将数组中每个元素及其索引相互对应
- 以空间换时间,将查找时间从 O(N) 降低到 O(1)

## 代码
* 语言支持:JS

```js
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
const twoSum = function (nums, target) {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
const diff = target - nums[i];
if (map.has(diff)) {
return [map.get(diff), i];
}
map.set(nums[i], i);
}
}
```

***复杂度分析***

- 时间复杂度:O(N)
- 空间复杂度:O(N)
77 changes: 77 additions & 0 deletions problems/17.Letter-Combinations-of-a-Phone-Number.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
## 题目地址
https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number

## 题目描述
```
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
示例:
输入:"23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
说明:
尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。
```

## 思路
使用回溯法进行求解,回溯是一种通过穷举所有可能情况来找到所有解的算法。如果一个候选解最后被发现并不是可行解,回溯算法会舍弃它,并在前面的一些步骤做出一些修改,并重新尝试找到可行解。究其本质,其实就是枚举。

如果没有更多的数字需要被输入,说明当前的组合已经产生。

如果还有数字需要被输入:
- 遍历下一个数字所对应的所有映射的字母
- 将当前的字母添加到组合最后,也就是 str + tmp[r]

## 关键点
利用回溯思想解题,在for循环中调用递归。

## 代码
* 语言支持:JS

```js
/**
* @param {string} digits
* @return {string[]}
*/
const letterCombinations = function (digits) {
if (!digits) {
return [];
}
const len = digits.length;
const map = new Map();
map.set('2', 'abc');
map.set('3', 'def');
map.set('4', 'ghi');
map.set('5', 'jkl');
map.set('6', 'mno');
map.set('7', 'pqrs');
map.set('8', 'tuv');
map.set('9', 'wxyz');
const result = [];

function generate(i, str) {
if (i == len) {
result.push(str);
return;
}
const tmp = map.get(digits[i]);
for (let r = 0; r < tmp.length; r++) {
generate(i + 1, str + tmp[r]);
}
}
generate(0, '');
return result;
};
```

***复杂度分析***

N + M 是输入数字的总数

- 时间复杂度:O(3^N * 4^M)
- 空间复杂度:O(3^N * 4^M)

63 changes: 63 additions & 0 deletions problems/21.MergeTwoSortedLists.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
## 题目地址
https://leetcode-cn.com/problems/merge-two-sorted-lists

## 题目描述
```
将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 
示例:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
```

## 思路
使用递归来解题,将两个链表头部较小的一个与剩下的元素合并,并返回排好序的链表头,当两条链表中的一条为空时终止递归。


## 关键点

- 掌握链表数据结构
- 考虑边界情况

## 代码
* 语言支持:JS

```js
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
const mergeTwoLists = function (l1, l2) {
if (l1 === null) {
return l2;
}
if (l2 === null) {
return l1;
}
if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else {
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
};
```

***复杂度分析***

M、N 是两条链表 l1、l2 的长度

- 时间复杂度:O(M+N)
- 空间复杂度:O(M+N)

72 changes: 72 additions & 0 deletions problems/22.GenerateParentheses.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
## 题目地址
https://leetcode-cn.com/problems/generate-parentheses

## 题目描述
```
数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。
示例:
输入:n = 3
输出:[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
```

## 思路

深度优先搜索(回溯思想),从空字符串开始构造,做加法。


## 关键点

- 当 l < r 时记得剪枝


## 代码
* 语言支持:JS

```js
/**
* @param {number} n
* @return {string[]}
* @param l 左括号已经用了几个
* @param r 右括号已经用了几个
* @param str 当前递归得到的拼接字符串结果
* @param res 结果集
*/
const generateParenthesis = function (n) {
const res = [];

function dfs(l, r, str) {
if (l == n && r == n) {
return res.push(str);
}
// l 小于 r 时不满足条件 剪枝
if (l < r) {
return;
}
// l 小于 n 时可以插入左括号,最多可以插入 n 个
if (l < n) {
dfs(l + 1, r, str + '(');
}
// r < l 时 可以插入右括号
if (r < l) {
dfs(l, r + 1, str + ')');
}
}
dfs(0, 0, '');
return res;
};
```


***复杂度分析***

- 时间复杂度:O(2^N)
- 空间复杂度:O(2^N)
Loading

0 comments on commit f04b4f9

Please sign in to comment.