<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刷題實(shí)戰(zhàn)26:刪除排序數(shù)組中的重復(fù)項(xiàng)

          共 1523字,需瀏覽 4分鐘

           ·

          2020-09-02 00:04

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


          今天和大家聊的問(wèn)題叫做?刪除排序數(shù)組中的重復(fù)項(xiàng),我們先來(lái)看題面:

          https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/

          Given a sorted array nums, remove the duplicates in-place such that each element appear only once 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.

          題意


          給定一個(gè)排序數(shù)組,你需要在 原地 刪除重復(fù)出現(xiàn)的元素,使得每個(gè)元素只出現(xiàn)一次,返回移除后數(shù)組的新長(zhǎng)度。
          不要使用額外的數(shù)組空間,你必須在 原地 修改輸入數(shù)組 并在使用 O(1) 額外空間的條件下完成。

          樣例


          示例?1:

          給定數(shù)組 nums = [1,1,2],

          函數(shù)應(yīng)該返回新的長(zhǎng)度 2, 并且原數(shù)組 nums 的前兩個(gè)元素被修改為 1, 2。

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


          示例?2:

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

          函數(shù)應(yīng)該返回新的長(zhǎng)度 5, 并且原數(shù)組 nums 的前五個(gè)元素被修改為 0, 1, 2, 3, 4。

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

          題解

          數(shù)組完成排序后,我們可以放置兩個(gè)指針 i 和 j,其中 i 是慢指針,而 j是快指針。只要 nums[i] = nums[j]? ,我們就增加 j 以跳過(guò)重復(fù)項(xiàng)。
          當(dāng)我們遇到 nums[j] ≠nums[i]?時(shí),跳過(guò)重復(fù)項(xiàng)的運(yùn)行已經(jīng)結(jié)束,因此我們必須把它(nums[j])的值復(fù)制到 nums[i + 1]。然后遞增 i,接著我們將再次重復(fù)相同的過(guò)程,直到 j 到達(dá)數(shù)組的末尾為止。
          時(shí)間復(fù)雜度:O(n),假設(shè)數(shù)組的長(zhǎng)度是 n,那么 i 和 j 分別最多遍歷 n 步。
          空間復(fù)雜度:O(1)。

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


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

          上期推文:


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


          瀏覽 52
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <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>
                  欧美性生交大片免费看APP麻豆 | 色婷婷18禁| 在线观看无码AV | 九九久久精品视频 | 成人欧美69口爆一区 |