js in-place algorithm All In One
js in-place algorithm All In One
solutions
- j = i + 1
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var sortColors = function(nums) {
const len = nums.length;
for (let i = 0; i < len; i++) {
for (let j = i + 1; j < len; j++) {
const temp = nums[i];
const current = nums[j];
//log('temp =', temp,';current =', current);
if(temp > current) {
// swap
nums[i] = current;
nums[j] = temp;
}
//log('nums =', nums);
}
//log('\n');
}
};
- j = 1
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var sortColors = function(nums) {
const len = nums.length;
for (let i = 0; i < len; i++) {
for (let j = i; j < len; j++) {
const temp = nums[i];
const current = nums[j];
//log('temp =', temp,';current =', current);
if(temp > current) {
// swap
nums[i] = current;
nums[j] = temp;
}
//log('nums =', nums);
}
//log('\n');
}
};
- j = 0
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var sortColors = function(nums) {
for (let index = 0; index < nums.length; index++) {
for (let j = 0; j < nums.length; j++) {
const temp = nums[index];
const current = nums[j];
// ❓ bigger
if(temp < current) {
// swap
nums[index] = current;
nums[j] = temp;
}
}
}
};
demo
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2022-01-11
* @modified
*
* @description 75. Sort Colors
* @description 75. 颜色分类
* @difficulty Medium
* @complexity O(n)
* @time O(n)
* @augments
* @example
* @link https://leetcode.com/problems/sort-colors/
* @link https://leetcode-cn.com/problems/sort-colors/
* @solutions
*
* @best_solutions
*
*/
const log = console.log;
// @decorator
const performanceTest = async (func, args, order = '') => {
const start = Date.now();
await func(args);
const end = Date.now();
log(`cost ${order} =`, (end - start) / 1000);
};
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var sortColors = function(nums) {
const len = nums.length;
for (let i = 0; i < len; i++) {
for (let j = i + 1; j < len; j++) {
const temp = nums[i];
const current = nums[j];
// log('temp =', temp,';current =', current);
if(temp > current) {
// swap
nums[i] = current;
nums[j] = temp;
}
// log('nums =', nums);
}
// log('\n');
}
log('1 =', nums);
};
var sortColors2 = function(nums) {
const len = nums.length;
for (let i = 0; i < len; i++) {
for (let j = i; j < len; j++) {
const temp = nums[i];
const current = nums[j];
// log('temp =', temp,';current =', current);
if(temp > current) {
// swap
nums[i] = current;
nums[j] = temp;
}
// log('nums =', nums);
}
// log('\n');
}
log('2 =', nums);
};
var sortColors3 = function(nums) {
const len = nums.length;
for (let i = 0; i < len; i++) {
for (let j = 0; j < len; j++) {
const temp = nums[i];
const current = nums[j];
// log('temp =', temp,';current =', current);
// ?
if(temp < current) {
// swap
nums[i] = current;
nums[j] = temp;
}
// log('nums =', nums);
}
// log('\n');
}
log('3 =', nums);
};
const nums = [ 2, 0, 2, 1, 1, 0]
// [0, 0, 1, 1, 2, 2]
performanceTest(sortColors, nums, 1);
performanceTest(sortColors2, nums, 2);
performanceTest(sortColors3, nums, 3);
// const test = sortColors(nums);
// const test2 = sortColors2(nums);
// const test3 = sortColors3(nums);
// log(`test =`, test);
// log(`test2 =`, test2);
// log(`test3 =`, test3);
refs
https://leetcode.com/problems/sort-colors/
https://leetcode.cn/problems/sort-colors/
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/15787877.html
未经授权禁止转载,违者必究!