成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專(zhuān)欄INFORMATION COLUMN

leedcode 數(shù)組:448. Find All Numbers Disappeared in a

zero / 1105人閱讀

摘要:題目描述思路先把數(shù)組進(jìn)行升序排序,再進(jìn)行數(shù)組去重,最后循環(huán)比較取得結(jié)果。升序排序可以使用若要降序排列可以則是數(shù)組去重,我使用的中的方法去重,可以參照一行代碼實(shí)現(xiàn)數(shù)組去重?cái)?shù)組去重?cái)?shù)組去重方法最優(yōu)解源碼排序去重

題目描述

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

Find all the elements of [1, n] inclusive that do not appear in this array.

Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

Example:

Input:
[4,3,2,7,8,2,3,1]

Output:
[5,6]

思路

先把數(shù)組進(jìn)行升序排序,再進(jìn)行數(shù)組去重,最后循環(huán)比較取得結(jié)果。

升序排序可以使用:

array.sort (function (a, b) {
    return a - b;  // 若要降序排列可以則是 b - a
});

數(shù)組去重,我使用的ES6中的Set方法去重,可以參照:
一行代碼實(shí)現(xiàn)數(shù)組去重(ES6)
JavaScript 數(shù)組去重
JS數(shù)組去重方法最優(yōu)解

源碼
/**
 * @param {number[]} nums
 * @return {number[]}
 */
var findDisappearedNumbers = function(nums) {
  // 排序
  let numList = nums.sort(function (a, b) {
    return a - b;
  });
  let numLength = nums.length;
  // 去重
  numList = Array.from(new Set(numList));
  let i = 0,
      a = [];

  for (let n = 1; n < numLength + 1; n++) {
    if (n > numList[numList.length - 1]) {
      a.push(n);
    } else {
      if (n == numList[i]) {
        i++;
      } else if (n < numList[i]) {
        a.push(n);
      }
    }
  }

  return a;
};

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/88068.html

相關(guān)文章

  • leetcode448. Find All Numbers Disappeared in an Ar

    摘要:題目要求假設(shè)一個(gè)長(zhǎng)度為的整數(shù)數(shù)組,數(shù)組中的元素的值位于區(qū)間中。代碼如下但是這個(gè)實(shí)現(xiàn)違背了的空間復(fù)雜度這里結(jié)果集不視為額外空間。如果當(dāng)前元素?zé)o需進(jìn)行交換,則指針右移一位。無(wú)需進(jìn)行的場(chǎng)景是指當(dāng)前元素已經(jīng)出現(xiàn)在目標(biāo)位置上了。 題目要求 Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some element...

    blankyao 評(píng)論0 收藏0
  • leetcode 448 Find All Numbers Disappeared in an Ar

    摘要:如果這個(gè)位置的值為正意味著我們還沒(méi)有對(duì)這個(gè)元素進(jìn)行過(guò)操作,我們將這個(gè)位置的元素的值取負(fù)。在整個(gè)遍歷結(jié)束后,沒(méi)有取負(fù)的值的索引,就可以對(duì)應(yīng)到?jīng)]有在數(shù)組出現(xiàn)過(guò)的值解法 題目詳情 Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others ap...

    MoAir 評(píng)論0 收藏0
  • 448. Find All Numbers Disappeared in an Array

    摘要:題目鏈接一般這種類(lèi)型的題要,要么給賦值成不在范圍內(nèi)的數(shù),要么到對(duì)應(yīng)位置。 448. Find All Numbers Disappeared in an Array 題目鏈接:https://leetcode.com/problems... 一般這種類(lèi)型的題要in place,要么給num[i]賦值成不在范圍內(nèi)的數(shù),要么swap到對(duì)應(yīng)位置。 public class Solution ...

    DevWiki 評(píng)論0 收藏0
  • [LeetCode] 448. Find All Numbers Disappeared in an

    Problem Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this array...

    X_AirDu 評(píng)論0 收藏0
  • [LeetCode] 448. Find All Numbers Disappeared in an

    Problem Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this array...

    Scorpion 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<