• TypeScript declare Object Array Interface methods All In One


    TypeScript declare Object Array Interface methods All In One

    extends Array<ObjectType>

    
    // type alias
    type ObjectType = {
      // input: [];
      // input: any[];
      input: [number[], number];
      result: number[];
      desc: string;
    }
    
    // 1. TypeScript & define Object Array Interface methods ✅ extends Array<ObjectType>
    interface TestCaseInterface extends Array<ObjectType> {
      //
    }
    
    // 测试用例 test cases
    const testCases: TestCaseInterface = [
      {
        input: [
          [1,2,3,4,5,6,7],
          3,
        ],
        result: [5,6,7,1,2,3,4],
        desc: 'value equal to [5,6,7,1,2,3,4]',
      },
      {
        input: [
          [-1,-100,3,99],
          2,
        ],
        result: [3,99,-1,-100],
        desc: 'value equal to [3,99,-1,-100]',
      },
    ];
    
    // Property 'entries' does not exist on type 'TestCaseInterface'.ts(2339)
    for (const [i, testCase] of testCases.entries()) {
      // const [first, second] = testCase.input;
      // Type '{ nums: number[]; k: number; }' must have a '[Symbol.iterator]()' method that returns an iterator.ts(2488)
      const [first, second] = testCase.input;
      rotate(first, second);
      // Argument of type 'number[]' is not assignable to parameter of type 'number'.ts(2345)
      log(`test case i result: \n`, first.join('') === testCase.result.join('') ? `passed ✅` : `failed ❌`, testCase.result);
    }
    
    

    extends Array<any> & [index: number]: ObjectType;

    
    // type alias
    type ObjectType = {
      // input: [];
      // input: any[];
      input: [number[], number];
      result: number[];
      desc: string;
    }
    
    // 2. TypeScript & define Object Array Interface methods ✅ [index: number]: ObjectType;
    interface TestCaseInterface extends Array<any> {
      [index: number]: ObjectType;
    }
    
    // 测试用例 test cases
    const testCases: TestCaseInterface = [
      {
        input: [
          [1,2,3,4,5,6,7],
          3,
        ],
        result: [5,6,7,1,2,3,4],
        desc: 'value equal to [5,6,7,1,2,3,4]',
      },
      {
        input: [
          [-1,-100,3,99],
          2,
        ],
        result: [3,99,-1,-100],
        desc: 'value equal to [3,99,-1,-100]',
      },
    ];
    
    // Property 'entries' does not exist on type 'TestCaseInterface'.ts(2339)
    for (const [i, testCase] of testCases.entries()) {
      // const [first, second] = testCase.input;
      // Type '{ nums: number[]; k: number; }' must have a '[Symbol.iterator]()' method that returns an iterator.ts(2488)
      const [first, second] = testCase.input;
      rotate(first, second);
      // Argument of type 'number[]' is not assignable to parameter of type 'number'.ts(2345)
      log(`test case i result: \n`, first.join('') === testCase.result.join('') ? `passed ✅` : `failed ❌`, testCase.result);
    }
    
    
    

    Property 'entries' does not exist on type ''.ts(2339)

    declare global {
      interface FormData {
        entries(): Iterator<[USVString, USVString | Blob]>;
      }
    }
    
    

    https://stackoverflow.com/questions/50677868/error-ts2339-property-entries-does-not-exist-on-type-formdata

    Type '' must have a '[Symbol.iterator]()' method that returns an iterator.ts(2488)

    tsconfig.json

    {
      "compilerOptions": {
        "target": "ES2022",
        "module": "ESNext",
        "lib": [
          "ES2015",
          "ES2016",
          "ES2017",
          "ES2018",
          "ES2019",
          "ES2020",
          "ES2021",
          "ES2022",
          "dom",
          "dom.iterable"
        ],
      }
    }
    
    
    

    leetcode demo

    "use strict";
    
    /**
     *
     * @author xgqfrms
     * @license MIT
     * @copyright xgqfrms
     * @created 2022-08-10
     * @modified
     *
     * @description 89. Rotate Array
     * @description 189. 旋转数组
     * @difficulty Medium
     * @ime_complexity O(n)
     * @space_complexity O(n)
     * @augments
     * @example
     * @link https://leetcode.com/problems/rotate-array/
     * @link https://leetcode-cn.com/problems/rotate-array/
     * @solutions
     *
     * @best_solutions
     *
     */
    
    export {};
    
    const log = console.log;
    
    /**
     Do not return anything, modify nums in-place instead.
     */
    
    // solution 2: in-place 就地交换算法 ✅
    function reverse(i: number, j: number, nums: number[]): void {
      while(i < j){
        [
          nums[i],
          nums[j]
        ] = [
          nums[j],
          nums[i]
        ];
        // 两两互换
        i++;
        j--;
      }
    };
    
    function rotate(nums: number[], k: number): void {
      k %= nums.length;
      // 如果 k 大于 nums.length 则完成一个循环,这意味着它将保持不变,我们必须进行剩余移位
      // reverse all
      reverse(0, nums.length-1, nums);
      // reverse first part
      reverse(0, k - 1, nums)
      // reverse second part
      reverse(k, nums.length-1, nums);
    };
    
    /*
    Line 10: Char 22: error TS2462: A rest element must be last in a destructuring pattern.
    
    */
    
    // type alias
    type ObjectType = {
      // input: [];
      // input: any[];
      input: [number[], number];
      result: number[];
      desc: string;
    }
    
    // 1. TypeScript & define Object Array Interface methods ✅ extends Array<ObjectType>
    // interface TestCaseInterface extends Array<ObjectType> {
    //   //
    // }
    
    // 2. TypeScript & define Object Array Interface methods ✅ [index: number]: ObjectType;
    interface TestCaseInterface extends Array<any> {
      [index: number]: ObjectType;
    }
    
    // 测试用例 test cases
    const testCases: TestCaseInterface = [
      {
        input: [
          [1,2,3,4,5,6,7],
          3,
        ],
        result: [5,6,7,1,2,3,4],
        desc: 'value equal to [5,6,7,1,2,3,4]',
      },
      {
        input: [
          [-1,-100,3,99],
          2,
        ],
        result: [3,99,-1,-100],
        desc: 'value equal to [3,99,-1,-100]',
      },
    ];
    // Property 'entries' does not exist on type 'TestCaseInterface'.ts(2339)
    for (const [i, testCase] of testCases.entries()) {
      // const [first, second] = testCase.input;
      // Type '{ nums: number[]; k: number; }' must have a '[Symbol.iterator]()' method that returns an iterator.ts(2488)
      const [first, second] = testCase.input;
      rotate(first, second);
      // Argument of type 'number[]' is not assignable to parameter of type 'number'.ts(2345)
      log(`test case i result: \n`, first.join('') === testCase.result.join('') ? `passed ✅` : `failed ❌`, testCase.result);
      // const result = rotate(first, second);
      // log(`test case i result: \n`, result.join('') === testCase.result.join('') ? `passed ✅` : `failed ❌`, result);
      // log(`test case i =`, testCase);
    }
    
    
    
    // solution 1:暴力破解:❌ Time Limit Exceeded
    // function rotate(nums: number[], k: number): void {
    //   if(k === 0) {
    //     // return;
    //   } else {
    //     const len = nums.length;
    //     while(k > 0) {
    //       // deep clone
    //       let temp = [];
    //       for(let i= 0; i< len; i++) {
    //         // 破解对象引用 bug, 值引用
    //         temp[i] = nums[i];
    //       }
    //       let end = temp[len - 1];
    //       // 循环队列
    //       for(let i = 1; i< len; i++) {
    //         // 交换swap
    //         nums[i] = temp[i - 1];
    //       }
    //       nums[0] = end;
    //       // console.log(`nums =`, nums);
    //       k -= 1;
    //     }
    //   }
    //  // no return, in-place 就地交换
    // };
    
    

    refs

    https://github.com/xgqfrms/leetcode/blob/master/js-solutions/189 rotate-array.ts

    https://raw.githubusercontent.com/xgqfrms/leetcode/master/js-solutions/189 rotate-array.ts?token=GHSAT0AAAAAABOKEIGRO3I5LZFGOZJXNELIYXUZMOQ



    ©xgqfrms 2012-2020

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

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


  • 相关阅读:
    postfix 邮件中继配置
    shell脚本网络流量实时查看
    zabbix配置邮件报警(第四篇)
    pptp服务故障
    Centos6.7 ELK日志系统部署
    rrdtool 实践
    Centos6.7安装Cacti教程
    Nagios事件机制实践
    Nrpe 插件安装教程
    如何查找一个命令由哪个rpm安装&&rpm 的相关查询方法
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/16576871.html
Copyright © 2020-2023  润新知