75.颜色分类
小于 1 分钟
75.颜色分类
/*
* @lc app=leetcode.cn id=75 lang=typescript
*
* [75] 颜色分类
*/
// @lc code=start
/**
Do not return anything, modify nums in-place instead.
*/
function sortColors(nums: number[]): void {
let l = 0,
r = nums.length;
while (l <= r) {
let tag = true;
for (let i = 0; i < r - 1; i++) {
// 左右对比,较大的放在右侧
if (nums[i] > nums[i + 1]) {
[nums[i], nums[i + 1]] = [nums[i + 1], nums[i]];
tag = false;
}
}
if (tag) break;
r--;
}
}
sortColors([2, 0, 2, 1, 1, 0]);
// @lc code=end
Loading...
