forked from azl397985856/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
luzhipeng
committed
Apr 14, 2019
1 parent
524a101
commit 808d840
Showing
13 changed files
with
487 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
``` | ||
|
Oops, something went wrong.