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
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2021-01-11 TypeScript Version 23 Design Patterns All In One
2021-01-11 Web 前端如何一键开启上帝模式 All In One
2021-01-11 Sentry 中文版
2021-01-11 前端 Web 异常监控系统 All In One
2021-01-11 Vue & Sentry sourcemaps All In One
2021-01-11 webpack OSS All In One
2021-01-11 Node.js 实战 & 最佳 Express 项目架构