File tree Expand file tree Collapse file tree 3 files changed +45
-0
lines changed Expand file tree Collapse file tree 3 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -13,3 +13,10 @@ LeetCode Problems' Solutions in JavaScript
13
13
7 . [ 从排序数组中删除重复项] ( ./algorithms/easy/removeDuplicates/removeDuplicates.js )
14
14
8 . [ 反转整数] ( ./algorithms/easy/reverse/reverse.js )
15
15
9 . [ 第一个错误的版本] ( ./algorithms/easy/solution/solution.js )
16
+
17
+ ## medium
18
+
19
+ 1 . [ 数组中的第 K 个最大元素] ( ./algorithms/medium/findKthLargest/findKthLargest.js )
20
+ 1 . [ 分类颜色] ( ./algorithms/medium/sortColors/sortColors.js )
21
+
22
+ ## hard
Original file line number Diff line number Diff line change
1
+ // description: https://leetcode-cn.com/explore/interview/card/top-interview-questions-medium/50/sorting-and-searching/98/
2
+ // 数组中的第K个最大元素
3
+
4
+ /**
5
+ * @param {number[] } nums
6
+ * @param {number } k
7
+ * @return {number }
8
+ */
9
+ var findKthLargest = function ( nums , k ) {
10
+ var snums = nums . sort ( ( a , b ) => a - b ) ;
11
+ return snums [ nums . length - k ] ;
12
+ } ;
Original file line number Diff line number Diff line change
1
+ // description: https://leetcode-cn.com/explore/interview/card/top-interview-questions-medium/50/sorting-and-searching/96/
2
+ // 分类颜色
3
+
4
+ // TODO: 一个仅使用常数空间的一趟扫描算法
5
+
6
+ /**
7
+ * @param {number[] } nums
8
+ * @return {void } Do not return anything, modify nums in-place instead.
9
+ */
10
+ var sortColors = function ( nums ) {
11
+ var counts = [ 0 , 0 , 0 ] ;
12
+ var i , count ;
13
+ var j = 0 ;
14
+ var len = nums . length ;
15
+
16
+ for ( i = 0 ; i < len ; i ++ ) {
17
+ counts [ nums [ i ] ] = counts [ nums [ i ] ] + 1 ;
18
+ }
19
+
20
+ for ( i = 0 , len = counts . length ; i < len ; i ++ ) {
21
+ count = counts [ i ] ;
22
+ while ( count -- ) {
23
+ nums [ j ++ ] = i ;
24
+ }
25
+ }
26
+ } ;
You can’t perform that action at this time.
0 commit comments