Removes the duplicate entry leetcode-26 from the sorted array

Notice it’s an ordered array

  componentDidMount() {
    const nums = [0.0.1.1.1.2.2.3.3.4];
    const result = this.fn2(nums);
    console.log(nums, result);
  }

  / * * *@description Deleting duplicate values * splice changes the array * */
  fn = (nums: number[]) = > {
    for (let i = 0; i < nums.length; i++) {
      if(nums.indexOf(nums[i]) ! == i) { nums.splice(i,1);
        i--; // Drop one space after delete}}return nums.length;
  };

  / * * *@description Moving element I will only default a pointer to len *. If len * does not have the same value, replace the current value with * */
  fn2 = (nums: number[]) = > {
    if(! nums.length) {return 0;
    }

    let len = 1;
    for (let i = 1; i < nums.length; i++) {
      if(nums[i] ! == nums[i -1]) { nums[len++] = nums[i]; }}return len;
  };
Copy the code

Look for the central index leetcode-724

We define an array center index by saying that the sum of all elements to the left of the array center index is equal to the sum of all elements to the right.

  / * * *@description 940ms * Affects slice and reduce each time the function is executed
  const fn = (arr: number[]) = > {
    let idx = -1;
    if (arr.length < 3) return idx;

    const sum = arr.reduce((pre, cur) = > pre + cur, 0);

    arr.forEach((item, index) = > {
      const splitArr = arr.slice(0, index);
      const newSum = splitArr.reduce((pre, cur) = > pre + cur, 0);

      if ((sum - item) / 2 === newSum && idx === -1) { idx = index; }});return idx;
  };

  / * * *@description Improved version 70ms */
  const fn2 = (arr: number[]) = > {
    let idx = -1,
      leftNum = 0;
    if (arr.length < 3) return idx;

    const sum = arr.reduce((pre, cur) = > pre + cur, 0);

    for (let i = 0; i < arr.length; i++) {
      if ((sum - arr[i]) / 2 === leftNum) {
        idx = i;
        break;
      } else{ leftNum += arr[i]; }}return idx;
  };

  console.log(fn2([-1, -1.0.0, -1, -1]));
  console.log(fn);
Copy the code

The array is at least twice as large as any other number leetcode-747

In a given array NUMs, there is always a maximum element. Find if the largest element in the array is at least twice every other number in the array. If it is, the index of the largest element is returned, otherwise -1 is returned. Input: nums = [3, 6, 1, 0] Output: 1 Description: 6 is the largest integer. For any other integer in the array, 6 is greater than twice the number of any other element in the array. The index of 6 is 1, so we return 1.

 const Basic: FC = () = > {
  / * * *@description The array comes with the method * */
  const fn = (nums: number[]): number= > {
    if (nums.length === 0) return -1;

    const max = Math.max.apply(null, nums);
    const idx = nums.findIndex(item= > item === max);
    const flag = nums.every((item, index) = > {
      return idx === index ? true : max >= 2 * item;
    });
    return flag ? idx : -1;
  };

  /* * @description * */
  const fn2 = (nums: number[]): number= > {
    if (nums.length === 0) return -1;
    let max = 0,
      secMax = 0,
      idx = 0;
    for (let i = 0; i < nums.length; i++) {
      /** Swap values */
      if (nums[i] > max) {
        secMax = max;
        max = nums[i];
        idx = i;
      } else if(nums[i] > secMax) { secMax = nums[i]; }}return max >= 2 * secMax ? idx : -1;
  };

  const result = fn2([0.0.2.2]);
  console.log(result);
  console.log(fn);

  return <Card>At least twice as large as any other array</Card>;
};
Copy the code

Add a leetcode – 66

/ * * *@description Increments one * Given a non-negative integer represented by a non-empty array of integers, increments one to that number. The highest digit is stored at the beginning of an array, where each element stores only a single digit. You can assume that this integer will not start with zero except for the integer 0 input: [1,2,3] output: [1,2,4] explanation: the input array represents the number 123 input: [4,3,2,1] output: [4,3,2,2] explanation: the input array represents the number 4321.@points Note that if converted to numbers: Js maximum limitation,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3 [6],1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,4 [6] 6145390195186705543 * * /
Copy the code
const Basic: FC = () = > {
  const fn = (nums: number[]): number[] => {
    let isCarry = false;

    for (let i = nums.length - 1; i > -1; i--) {
      if (isCarry || i === nums.length - 1) {
        nums[i] = nums[i] + 1;
      }

      if (nums[i] === 10) {
        isCarry = true;
        nums[i] = 0;
      } else {
        isCarry = false; }}if (isCarry) nums.unshift(1);

    return nums;
  };

  const result = fn([9]);
  console.log(result);

  return <>Gal.</>;
};
Copy the code