Skip to content

Commit 0042f0a

Browse files
committed
solutions
1 parent c764da1 commit 0042f0a

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,10 @@ LeetCode Problems' Solutions in JavaScript
1313
7. [从排序数组中删除重复项](./algorithms/easy/removeDuplicates/removeDuplicates.js)
1414
8. [反转整数](./algorithms/easy/reverse/reverse.js)
1515
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
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
};

0 commit comments

Comments
 (0)