日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LeetCode算法入门- Remove Duplicates from Sorted Array -day21

發布時間:2025/3/12 编程问答 11 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode算法入门- Remove Duplicates from Sorted Array -day21 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

LeetCode算法入門- Remove Duplicates from Sorted Array -day21

  • 題目描述
    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.

    Example 1:

    Given nums = [1,1,2],

    Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.

    It doesn’t matter what you leave beyond the returned length.

    Example 2:

    Given nums = [0,0,1,1,1,2,2,3,3,4],

    Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

    It doesn’t matter what values are set beyond the returned length.

  • 題目分析
    將一個數組重復的元素去掉,并返回數組長度。

  • Java實現
    定義兩個指針,i指針從0開始,j從1開始,如果nums[i]與nums[j]相等,則j向后移動,移到nums[i]與nums[j]不相等,這是才將
    nums[++i] = nums[j],最后返回i+1.

  • class Solution {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;} }

    總結

    以上是生活随笔為你收集整理的LeetCode算法入门- Remove Duplicates from Sorted Array -day21的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。