Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaeffd77

Browse files
committed
added 162, 73
1 parent17883d1 commitaeffd77

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

‎Medium/162-findPeakElement.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* key. binary search, why does it work?
3+
* explanation: https://leetcode.com/discuss/17793/find-the-maximum-by-binary-search-recursion-and-iteration
4+
*
5+
*@param {number[]} nums
6+
*@return {number}
7+
*/
8+
varfindPeakElement=function(nums){
9+
varlow=0;
10+
varhigh=nums.length-1;
11+
12+
while(low<high){
13+
varmid=low+Math.floor((high-low)/2);
14+
if(nums[mid+1]>nums[mid])low=mid+1;
15+
elsehigh=mid;
16+
}
17+
18+
returnhigh;
19+
};

‎Medium/73-setMatrixZeroes.js‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Key: use first row to store the zero state for each column exclude the first col,
3+
* use first col to store the zero state for each row exclude the first row
4+
* use two variable to mark whether the first row and the first column has zeros.
5+
* set the matrix with zeros with the states.
6+
*
7+
*@param {number[][]} matrix
8+
*@return {void} Do not return anything, modify matrix in-place instead.
9+
*/
10+
varsetZeroes=function(matrix){
11+
varfirstRowZero=false;
12+
varfirstColZero=false;
13+
varrows=matrix.length;
14+
varcols=matrix[0].length;
15+
16+
for(vari=0;i<rows;i++){
17+
for(varj=0;j<cols;j++){
18+
if(matrix[i][j]===0){
19+
if(i===0)firstRowZero=true;
20+
if(j===0)firstColZero=true;
21+
matrix[i][0]=0;
22+
matrix[0][j]=0;
23+
}
24+
}
25+
}
26+
27+
for(vari=1;i<rows;i++){
28+
for(varj=1;j<cols;j++){
29+
if(matrix[i][0]===0||matrix[0][j]===0){
30+
matrix[i][j]=0;
31+
}
32+
}
33+
}
34+
35+
if(firstRowZero){
36+
for(vari=0;i<cols;i++){
37+
matrix[0][i]=0;
38+
}
39+
}
40+
41+
if(firstColZero){
42+
for(vari=0;i<rows;i++){
43+
matrix[i][0]=0;
44+
}
45+
}
46+
};

‎README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
*[59. Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/) -[Solution](./Medium/59-spiralMatrixII.js)
7777
*[62.Unique Paths](https://leetcode.com/problems/unique-paths/) -[Solution](./Medium/62-uniquePaths.js)
7878
*[64.Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/) -[Solution](./Medium/64-minimumPathSum.js)
79+
*[73. Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) -[Solution](./Medium/73-setMatrixZeroes.js)
7980
*[74. Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) -[Solution](./Medium/74-search2DMatrix.js)
8081
*[75. Sort Colors](https://leetcode.com/problems/sort-colors/) -[Solution](./Medium/75-sortColors.js)
8182
*[77.Combinations](https://leetcode.com/problems/combinations/) -[Solution](./Medium/77-combinations.js)
@@ -90,6 +91,7 @@
9091
*[141. Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) -[Solution](./Medium/141-linkedListCycle.js)
9192
*[144. Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) -[Solution](./Medium/144-binaryTreePreorder.js)
9293
*[153.Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) -[Solution](./Medium/153-findMinimumInRotatedSortedArray.js)
94+
*[162. Find Peak Element](https://leetcode.com/problems/find-peak-element/) -[Solution](./Medium/162-findPeakElement.js)
9395
*[199. Binary Tree Right Side View](https://leetcode.com/problems/binary-tree-right-side-view/) -[Solution](./Medium/199-binaryTreeRightSideView.js)
9496
*[213. House Robber II](https://leetcode.com/problems/house-robber-ii/) -[Solution](./Medium/213-houseRobberII.js)
9597
*[230. Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/) -[Solution](./Medium/230-kthSmallestElementinBST.js)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp