<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)27:移除元素

          共 1529字,需瀏覽 4分鐘

           ·

          2020-09-04 23:12

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !


          今天和大家聊的問題叫做?移除元素,我們先來看題面:

          https://leetcode-cn.com/problems/remove-element/

          Given an array nums and a value val, remove all instances of that value in-place and return the new length.

          Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

          The order of elements can be changed. It doesn't matter what you leave beyond the new length.

          題意


          給你一個數(shù)組 nums 和一個值 val,你需要 原地 移除所有數(shù)值等于 val 的元素,并返回移除后數(shù)組的新長度。

          不要使用額外的數(shù)組空間,你必須僅使用 O(1) 額外空間并 原地 修改輸入數(shù)組。

          元素的順序可以改變。你不需要考慮數(shù)組中超出新長度后面的元素。

          樣例


          示例 1:

          給定 nums = [3,2,2,3], val = 3,

          函數(shù)應(yīng)該返回新的長度 2, 并且 nums 中的前兩個元素均為 2。

          你不需要考慮數(shù)組中超出新長度后面的元素。


          示例?2:

          給定 nums = [0,1,2,2,3,0,4,2], val = 2,

          函數(shù)應(yīng)該返回新的長度 5, 并且 nums 中的前五個元素為 0, 1, 3, 0, 4。

          注意這五個元素可為任意順序。

          你不需要考慮數(shù)組中超出新長度后面的元素。

          題解

          當(dāng) nums[j] 與給定的值相等時,遞增 j 以跳過該元素。只要 nums[j] ≠ val ,我們就復(fù)制 nums[j]?到 nums[i] 并同時遞增兩個索引。重復(fù)這一過程,直到 j 到達(dá)數(shù)組的末尾,該數(shù)組的新長度為 i。
          時間復(fù)雜度:O(n),假設(shè)數(shù)組總共有?n?個元素,i和?j至少遍歷 2n步2n2n步。
          空間復(fù)雜度:O(1)。
          該解法與?刪除排序數(shù)組中的重復(fù)項?的解法十分相似。

          public?int?removeElement(int[] nums, int?val)?{
          ????int?i = 0;
          ????for?(int?j = 0; j < nums.length; j++) {
          ????????if?(nums[j] != val) {
          ????????????nums[i] = nums[j];
          ????????????i++;
          ????????}
          ????}
          ????return?i;
          }

          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力。

          上期推文:


          LeetCode1-20題匯總,速度收藏!
          LeetCode刷題實戰(zhàn)21:合并兩個有序鏈表
          LeetCode刷題實戰(zhàn)23:合并K個升序鏈表
          LeetCode刷題實戰(zhàn)24:兩兩交換鏈表中的節(jié)點
          LeetCode刷題實戰(zhàn)25:K 個一組翻轉(zhuǎn)鏈表


          瀏覽 33
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  少妇被c 黄 在线网站动漫 | 美女在线扣穴 | 靠逼视频网站在线观看 | 欧美在线视频99 | 99青草在线免费观看视频 |