-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort_Colors.js
More file actions
41 lines (38 loc) · 1.03 KB
/
Copy pathSort_Colors.js
File metadata and controls
41 lines (38 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* 75. Sort Colors
*
* Tag: Array、Two Pointers、Sort
*
* https://leetcode.com/problems/sort-colors/description/
*/
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var sortColors = function(nums) {
if (nums.length > 1) {
var i = 0; // 当前位置
var frontIndex = 0; // 前坑位
var endIndex = nums.length - 1; // 后坑位
while (i <= endIndex) { // 遍历数组, 把0、2替换到前后坑位上
if (nums[i] == 0 && i != frontIndex) {
swapElement(nums, i, frontIndex);
frontIndex++;
} else if (nums[i] == 2 && i != endIndex) {
swapElement(nums, i, endIndex);
endIndex--;
} else {
i++;
}
}
}
return nums;
};
function swapElement(nums, i, j) {
nums[i] ^= nums[j];
nums[j] = nums[i] ^ nums[j];
nums[i] = nums[i] ^ nums[j];
}
let elems = [2,0,1];
sortColors(elems);
console.log(elems);