leetcode-pp / 91alg-11-daily-check

第十一期打卡
3 stars 0 forks source link

【Day 26 】2023-07-05 - 26.删除排序数组中的重复项 #27

Open azl397985856 opened 1 year ago

azl397985856 commented 1 year ago

26.删除排序数组中的重复项

入选理由

暂无

题目地址

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

前置知识

暂无

题目描述

给定一个排序数组,你需要在 原地 删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。

不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。

示例 1:

给定数组 nums = [1,1,2],

函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。

你不需要考虑数组中超出新长度后面的元素。
示例 2:

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

函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。

你不需要考虑数组中超出新长度后面的元素。

说明:

为什么返回数值是整数,但输出的答案是数组呢?

请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。

你可以想象内部操作如下:

// nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
int len = removeDuplicates(nums);

// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。
for (int i = 0; i < len; i++) {
    print(nums[i]);
}
hengistchan commented 1 year ago

双指针,重拳出击

function removeDuplicates(nums: number[]): number {
  let i = 1, cur = nums[0], j = 0
  while (i < nums.length) {
    if (nums[i] !== cur) {
      nums[j + 1] = nums[i]
      j++
      cur = nums[i]
    }
    i++
  }
  return nums.length - i + j + 1
};
SoSo1105 commented 1 year ago

思路

双指针法

代码

class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        p = 0
        for i in range(1, len(nums)):
            if nums[i] == nums[i-1]:
               continue
            else:
                p += 1
                nums[p] = nums[i]
        # 题目要求返回的是长度,所有坐标要加 1
        return p + 1

复杂度分析

Diana21170648 commented 1 year ago

思路

用快慢指针(读写指针)

代码

class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        left=right=0
        if not nums:
            return 0
        while right<len(nums):
            if nums[right]!=nums[left]:#如果不相等,则读指针后移,写指针也后
                left+=1
                nums[left]=nums[right]#保证最后返回的nums不重复
            right+=1
        return left+1#索引从0开始

复杂度分析

zhaoygcq commented 1 year ago

思路

双指针: 指针left指向数组的第一个元素 指针right指向数组的第二个元素

/**
 * @param {number[]} nums
 * @return {number}
 */
var removeDuplicates = function(nums) {
    if(nums.length < 1) return 1;
    let left = 0, right = 0;
    while(right < nums.length) {
        if(nums[left] == nums[right]) {
            right++;
        } else {
            nums[left + 1] = nums[right];
            left++;
            right++;
        }
    }
    return left + 1;
};

复杂度分析

GuitarYs commented 1 year ago
class Solution:
    # nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
    def removeDuplicates(self, nums):
        if not nums:
            return 0

        i = 0
        for j in range(1, len(nums)):
            if nums[j] != nums[i]:
                i += 1
                nums[i] = nums[j]

        return i + 1
solution = Solution()

nums1 = [1, 1, 2]
length1 = solution.removeDuplicates(nums1)
print("Modified Array 1:", nums1[:length1])

nums2 = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]
length2 = solution.removeDuplicates(nums2)
print("Modified Array 2:", nums2[:length2])
snmyj commented 1 year ago
class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
         int cnt=1;
         for(int i=0;i<nums.size()-1;i++){
             if(nums[i+1]!=nums[i]) cnt++;

         }
         int s=1;
         for(int i=1;i<nums.size();i++){
             if(nums[i]!=nums[i-1]) {
                 nums[s]=nums[i];
                 s++;
             }
         }
         return cnt;
    }
};
bi9potato commented 1 year ago

Approach

Double pointers

Code


class Solution {
    public int removeDuplicates(int[] nums) {

        int slow = 0, fast = slow+1;

        while (fast < nums.length) {
            while (fast < nums.length && nums[fast] == nums[slow]) {
                fast++;
            }
            if (fast < nums.length) {
                nums[++slow] = nums[fast];
            }
        }

        return slow+1;

    }
}

Complexity Analysis

catkathy commented 1 year ago

Code

class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        n = len(nums)
        j = 0

        for i in range(n):
            if nums[i] != nums[j]:
                j += 1
                nums[j] = nums[i]

        return j + 1
wzbwzt commented 1 year ago

/* 思路: 双指针 slow,fast

复杂度: 时间复杂度: O(n) 空间复杂度: O(1)

*/

func removeDuplicates(nums []int) int {
    if len(nums) == 0 {
        return 0
    }
    slow, fast := 0, 0
    for fast < len(nums) {
        if nums[slow] == nums[fast] {
            fast++
            continue
        }
        nums[slow+1] = nums[fast]
        fast++
        slow++
    }
    // nums = nums[:slow+1]
    return slow + 1
}
joemonkeylee commented 1 year ago
function removeDuplicates(nums: number[]): number {
    if (nums == null || nums.length == 0) return 0;
    let p = 0;
    let q = 1;
    while (q < nums.length) {
        if (nums[p] != nums[q]) {
            nums[p + 1] = nums[q];
            p++;
        }
        q++;
    }
    return p + 1;
};
Master-guang commented 1 year ago

/**
 * @param {number[]} nums
 * @return {number}
 */
var removeDuplicates = function(nums) {
    if(nums.length == 0) {
        return 0;
    }
    let slow = 0;
    let fast = 0;
    while(fast < nums.length) {
        if(nums[fast] != nums[slow]) {
            slow++
            nums[slow] = nums[fast]
        }
        fast++
    }
    return slow + 1
};
Alexno1no2 commented 1 year ago
class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        slow, fast = 0, 1
        while fast < len(nums):
            if nums[fast] != nums[slow]:
                slow = slow + 1
                # 维护 nums[0..slow] 无重复
                nums[slow] = nums[fast]
            fast = fast + 1
        return slow + 1
Fuku-L commented 1 year ago

代码

class Solution {
    public int removeDuplicates(int[] nums) {
        // 双指针
        int i = 0,j = 1;
        for(; j < nums.length;){
            if(nums[i] == nums[j]){
                j++;
            } else {
                nums[++i] = nums[j++];
            }
        }
        return i+1;
    }
}
yzhyzhyzh123 commented 1 year ago

代码

class Solution {
    public int removeDuplicates(int[] nums) {
        // 双指针,
        int n = nums.length;
        int fast = 1;
        int slow = 1;
        while (fast < n) {
            if (nums[fast -1] != nums[fast]) {
                nums[slow] = nums[fast];
                slow++;
            }
            fast++;

        }
        return slow ;
    }
}
Beanza commented 1 year ago

function removeDuplicates(nums: number[]): number { let i = 1, cur = nums[0], j = 0 while (i < nums.length) { if (nums[i] !== cur) { nums[j + 1] = nums[i] j++ cur = nums[i] } i++ } return nums.length - i + j + 1 };

954545647 commented 1 year ago

暴力和双子针写法

/**
 * @param {number[]} nums
 * @return {number}
 */
var removeDuplicates = function (nums) {
  let len = nums.length;
  if (len === 0 || len === 1) return nums.length;
  if (len === 2 && nums[0] !== nums[1]) return 2;
  let left = 0;
  let right = 1;
  while (right < len) {
    const letVal = nums[left];
    const rightVal = nums[right];
    if (letVal === rightVal) {
      nums.splice(right, 1);
      len--
    } else {
      left++;
      right++;
    }
  }
  return nums.length;
};

var removeDuplicates = function (nums) {
  const len = nums.length;
  if (len === 0 || len === 1) return nums.length;
  if (len === 2 && nums[0] !== nums[1]) return 2;
  let left = 0;
  let right = 1;
  while (right < len) {
    const letVal = nums[left];
    const rightVal = nums[right];
    if (letVal === rightVal) {
      right++
    } else {
      left++
      nums[left] = rightVal;
    }
  }
  return left + 1;
};
Moin-Jer commented 1 year ago
class Solution {
    public int removeDuplicates(int[] nums) {
        if (nums.length <= 1) {
            return nums.length;
        }
        int i = 0;
        int j = 1;
        while (j < nums.length) {
            if (nums[i] != nums[j]) {
                ++i;
                nums[i] = nums[j];
            }
            ++j;
        }
        return i + 1;
    }
}