zoukankan      html  css  js  c++  java
  • js inplace algorithm All In One

    js in-place algorithm All In One

    solutions

    1. 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');
      }
    };
    
    
    1. 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');
      }
    };
    
    
    1. 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



    ©xgqfrms 2012-2020

    www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

    原创文章,版权所有©️xgqfrms, 禁止转载 ️,侵权必究⚠️!


    xgqfrms
  • 相关阅读:
    2822 爱在心中
    P1707 刷题比赛
    1269 匈牙利游戏
    1482 路线统计
    Codevs 1287 矩阵乘法&&Noi.cn 09:矩阵乘法(矩阵乘法练手题)
    P2022 有趣的数
    1087 麦森数
    P1111 修复公路
    python为在线漫画站点自制非官方API(未完待续)
    逻辑运算0==x和x==0具体解释
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/15787877.html
Copyright © 2011-2022 走看看