Skip to content

Commit

Permalink
feat: 更新题目 169,226,240,887
Browse files Browse the repository at this point in the history
  • Loading branch information
luzhipeng committed Apr 14, 2019
1 parent 524a101 commit 808d840
Show file tree
Hide file tree
Showing 13 changed files with 487 additions and 197 deletions.
33 changes: 15 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ leetcode 题解,记录自己的 leecode 解题之路。
## 食用指南

- 经典题目的解析的目录部分,前面有🆕的代表是最新更新的
- 对于最近添加的部分, 前面会有 🆕 标注
- 对于最近更新的部分, 前面会有 🖊 标注
- 将来会在这里更新anki卡片
- 这里有一份leetcode官方账号在知乎上给出的一个《互联网公司最常见的面试算法题有哪些?》的答案,我这里尽量去覆盖回答中的题目和知识点
Expand Down Expand Up @@ -66,21 +66,25 @@ leetcode 题解,记录自己的 leecode 解题之路。

- [20. Valid Parentheses](./problems/validParentheses.md)
- [26.remove-duplicates-from-sorted-array](./problems/26.remove-duplicates-from-sorted-array.md)
- [206.reverse-linked-list](./problems/206.reverse-linked-list.md)
- [136.single-number](./problems/136.single-number.md)
- [167.two-sum-ii-input-array-is-sorted](./problems/167.two-sum-ii-input-array-is-sorted.md)
- 🆕 [169.majority-element](./problems/169.majority-element.md)
- [190.reverse-bits](./problems/190.reverse-bits.md)
- [191.number-of-1-bits](./problems/191.number-of-1-bits.md)
- [203.remove-linked-list-elements](./problems/203.remove-linked-list-elements.md)
- [206.reverse-linked-list](./problems/206.reverse-linked-list.md)
- [219.contains-duplicate-ii](./problems/219.contains-duplicate-ii.md)
- 🆕 [226.invert-binary-tree](./problems/226.invert-binary-tree.md)
- [283.move-zeroes](./problems/283.move-zeroes.md)
- [349.intersection-of-two-arrays](./problems/349.intersection-of-two-arrays.md)
- [190.reverse-bits](./problems/190.reverse-bits.md)
- [191.number-of-1-bits](./problems/191.number-of-1-bits.md)


#### 中等难度

- [2. Add Two Numbers](./problems/addTwoNumbers.md)
- [3. Longest Substring Without Repeating Characters](./problems/longestSubstringWithoutRepeatingCharacters.md)
- [5. Longest Palindromic Substring](./problems/longestPalindromicSubstring.md)
- 🆕 [11.container-with-most-water](./problems/11.container-with-most-water.md)
- [19. Remove Nth Node From End of List](./problems/removeNthNodeFromEndofList.md)
- [24. Swap Nodes In Pairs](./problems/swapNodesInPairs.md)
- [75.sort-colors.md](./problems/75.sort-colors.md)
Expand All @@ -91,18 +95,19 @@ leetcode 题解,记录自己的 leecode 解题之路。
- [103.binary-tree-zigzag-level-order-traversal](./problems/103.binary-tree-zigzag-level-order-traversal.md)
- [144.binary-tree-preorder-traversal](./problems/144.binary-tree-preorder-traversal.md)
- [150.evaluate-reverse-polish-notation](./problems/150.evaluate-reverse-polish-notation.md)
- [328.odd-even-linked-list](./problems/328.odd-even-linked-list.md)
- [445.add-two-numbers-ii](./problems/445.add-two-numbers-ii.md)
- [877.stone-game](./problems/877.stone-game.md)
- [279.perfect-squares](./problems/279.perfect-squares.md)
- [199.binary-tree-right-side-view](./problems/199.binary-tree-right-side-view.md)
- [201.bitwise-and-of-numbers-range](./problems/201.bitwise-and-of-numbers-range.md)
- [209.minimum-size-subarray-sum](./problems/209.minimum-size-subarray-sum.md)
- [900.rle-iterator](./problems/900.rle-iterator.md)
- 🆕 [240.search-a-2-d-matrix-ii](./problems/240.search-a-2-d-matrix-ii.md)
- [279.perfect-squares](./problems/279.perfect-squares.md)
- [322.coin-change](./problems/322.coin-change.md)
- [328.odd-even-linked-list](./problems/328.odd-even-linked-list.md)
- [445.add-two-numbers-ii](./problems/445.add-two-numbers-ii.md)
- [518.coin-change-2](./problems/518.coin-change-2.md)
- 🆕 [11.container-with-most-water](./problems/11.container-with-most-water.md)
- 🆕 [875.koko-eating-bananas](./problems/875.koko-eating-bananas.md)
- [877.stone-game](./problems/877.stone-game.md)
- 🆕 [887.super-egg-drop](./problems/887.super-egg-drop.md)
- [900.rle-iterator](./problems/900.rle-iterator.md)

#### 困难难度

Expand All @@ -124,22 +129,14 @@ TODO

### 计划

[226.invert-binary-tree]

[494.target-sum]

[88.merge-sorted-array]

[139.word-break]

[169.majority-element]

[240.search-a-2-d-matrix-ii]

[416.partition-equal-subset-sum]

[609.find-duplicate-file-in-system]

[887.super-egg-drop]

anki 卡片
Binary file added assets/problems/169.majority-element.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/problems/240.search-a-2-d-matrix-ii.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/problems/887.super-egg-drop-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/problems/887.super-egg-drop-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
96 changes: 96 additions & 0 deletions problems/169.majority-element.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@

## 题目地址
https://leetcode.com/problems/majority-element/description/

## 题目描述

```
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
You may assume that the array is non-empty and the majority element always exist in the array.
Example 1:
Input: [3,2,3]
Output: 3
Example 2:
Input: [2,2,1,1,1,2,2]
Output: 2
```

## 思路

符合直觉的做法是利用额外的空间去记录每个元素出现的次数,并用一个单独的变量记录当前出现次数最多的元素。

但是这种做法空间复杂度较高,有没有可能进行优化呢? 答案就是用"投票算法"。

投票算法的原理是通过不断消除不同元素直到没有不同元素,剩下的元素就是我们要找的元素。

![169.majority-element](../assets/problems/169.majority-element.png)

## 关键点解析

- 投票算法


## 代码

```js


/*
* @lc app=leetcode id=169 lang=javascript
*
* [169] Majority Element
*
* https://leetcode.com/problems/majority-element/description/
*
* algorithms
* Easy (51.62%)
* Total Accepted: 365.6K
* Total Submissions: 702.5K
* Testcase Example: '[3,2,3]'
*
* Given an array of size n, find the majority element. The majority element is
* the element that appears more than ⌊ n/2 ⌋ times.
*
* You may assume that the array is non-empty and the majority element always
* exist in the array.
*
* Example 1:
*
*
* Input: [3,2,3]
* Output: 3
*
* Example 2:
*
*
* Input: [2,2,1,1,1,2,2]
* Output: 2
*
*
*/
/**
* @param {number[]} nums
* @return {number}
*/
var majorityElement = function(nums) {
let count = 1;
let majority = nums[0];
for(let i = 1; i < nums.length; i++) {
if (count === 0) {
majority = nums[i];
}
if (nums[i] === majority) {
count ++;
} else {
count --;
}
}
return majority;
};
```

Original file line number Diff line number Diff line change
@@ -1,3 +1,46 @@

## 题目地址
https://leetcode.com/problems/invert-binary-tree/description/

## 题目描述

```
Invert a binary tree.
Example:
Input:
4
/ \
2 7
/ \ / \
1 3 6 9
Output:
4
/ \
7 2
/ \ / \
9 6 3 1
Trivia:
This problem was inspired by this original tweet by Max Howell:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off.
```

## 思路
遍历树(随便怎么遍历),然后将左右子树交换位置。
## 关键点解析

- 递归简化操作
- 如果树很高,建议使用栈来代替递归
- 这道题目对顺序没要求的,因此队列数组操作都是一样的,无任何区别

## 代码

```js

/*
* @lc app=leetcode id=226 lang=javascript
*
Expand Down Expand Up @@ -78,3 +121,4 @@ var invertTree = function(root) {
}
return root;
};
```
116 changes: 116 additions & 0 deletions problems/240.search-a-2-d-matrix-ii.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@

## 题目地址
https://leetcode.com/problems/search-a-2d-matrix-ii/description/

## 题目描述

```
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
Example:
Consider the following matrix:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.
Given target = 20, return false.
```

## 思路

符合直觉的做法是两层循环遍历,时间复杂度是O(m * n),
有没有时间复杂度更好的做法呢? 答案是有,那就是充分运用矩阵的特性(横向纵向都递增),
我们可以从角落(左下或者右上)开始遍历,这样时间复杂度是O(m + n).

![240.search-a-2-d-matrix-ii](../assets/problems/240.search-a-2-d-matrix-ii.png)

其中蓝色代表我们选择的起点元素, 红色代表目标元素。

## 关键点解析

- 从角落开始遍历,利用递增的特性简化时间复杂度


## 代码

```js

/*
* @lc app=leetcode id=240 lang=javascript
*
* [240] Search a 2D Matrix II
*
* https://leetcode.com/problems/search-a-2d-matrix-ii/description/
*
* algorithms
* Medium (40.30%)
* Total Accepted: 170K
* Total Submissions: 419.1K
* Testcase Example: '[[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]]\n5'
*
* Write an efficient algorithm that searches for a value in an m x n matrix.
* This matrix has the following properties:
*
*
* Integers in each row are sorted in ascending from left to right.
* Integers in each column are sorted in ascending from top to bottom.
*
*
* Example:
*
* Consider the following matrix:
*
*
* [
* ⁠ [1, 4, 7, 11, 15],
* ⁠ [2, 5, 8, 12, 19],
* ⁠ [3, 6, 9, 16, 22],
* ⁠ [10, 13, 14, 17, 24],
* ⁠ [18, 21, 23, 26, 30]
* ]
*
*
* Given target = 5, return true.
*
* Given target = 20, return false.
*
*/
/**
* @param {number[][]} matrix
* @param {number} target
* @return {boolean}
*/
var searchMatrix = function(matrix, target) {
if (!matrix || matrix.length === 0) return 0;

let colIndex = 0;
let rowIndex = matrix.length - 1;
while(rowIndex > 0 && target < matrix[rowIndex][colIndex]) {
rowIndex --;
}

while(colIndex < matrix[0].length) {
if (target === matrix[rowIndex][colIndex]) return true;
if (target > matrix[rowIndex][colIndex]) {
colIndex ++;
} else if (rowIndex > 0){
rowIndex --;
} else {
return false;
}
}

return false;
};
```

Loading

0 comments on commit 808d840

Please sign in to comment.