LeetCode //80. Remove Duplicates from Sorted Array II

这篇具有很好参考价值的文章主要介绍了LeetCode //80. Remove Duplicates from Sorted Array II。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

80. Remove Duplicates from Sorted Array II

Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same.

Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.

Return k after placing the final result in the first k slots of nums.

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

Custom Judge:

The judge will test your solution with the following code:

int[] nums = […]; // Input array
int[] expectedNums = […]; // The expected answer with correct length
 
int k = removeDuplicates(nums); // Calls your implementation
 
assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
      assert nums[i] == expectedNums[i];
}

If all assertions pass, then your solution will be accepted.
 

Example 1:

Input: nums = [1,1,1,2,2,3]
Output: 5, nums = [1,1,2,2,3,_]
Explanation: Your function should return k = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).

Example 2:

Input: nums = [0,0,1,1,1,1,2,3,3]
Output: 7, nums = [0,0,1,1,2,3,3,,]
Explanation: Your function should return k = 7, with the first seven elements of nums being 0, 0, 1, 1, 2, 3 and 3 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).

Constraints:
  • 1 < = n u m s . l e n g t h < = 3 ∗ 1 0 4 1 <= nums.length <= 3 * 10^4 1<=nums.length<=3104
  • − 1 0 4 < = n u m s [ i ] < = 1 0 4 -10^4 <= nums[i] <= 10^4 104<=nums[i]<=104
  • nums is sorted in non-decreasing order.

From: LeetCode
Link: 80. Remove Duplicates from Sorted Array II文章来源地址https://www.toymoban.com/news/detail-535494.html


Solution:

Ideas:
In this function, we start by checking if the array has less than or equal to 2 elements. If it does, we return numsSize since all elements are unique or appear at most twice.
Next, we initialize i and j to 2. We then start a loop that runs from i to numsSize - 1. Inside the loop, we check if the current element is different from the element at position j-2. If it is, we place it at the position pointed to by j and increment j.
Finally, we return j, which is the number of elements in the array after removing duplicates that appear more than twice.
This function modifies the array in-place and does not use any extra space, except for a few variables. The time complexity is O(n), where n is the size of the array.
Code:
int removeDuplicates(int* nums, int numsSize){
    if(numsSize <= 2) return numsSize;

    int i, j = 2;
    for(i = 2; i < numsSize; i++){
        if(nums[i] != nums[j-2]){
            nums[j] = nums[i];
            j++;
        }
    }

    return j;
}

到了这里,关于LeetCode //80. Remove Duplicates from Sorted Array II的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【算法】Two Sum II - Input Array Is Sorted 两数之和 II - 输入有序数组

    给你一个下标从 1 开始的整数数组 numbers ,该数组已按 非递减顺序排列 ,请你从数组中找出满足相加之和等于目标数 target 的两个数。如果设这两个数分别是 numbers[index1] 和 numbers[index2] ,则 1 = index1 index2 = numbers.length 。 以长度为 2 的整数数组 [index1, index2] 的形式返回这两个

    2024年02月13日
    浏览(48)
  • LeetCode //88. Merge Sorted Array

    You are given two integer arrays nums1 and nums2 , sorted in non-decreasing order, and two integers m and n , representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted in non-decreasing order. The final sorted array should not be returned by the function, but instead be stored inside the array nums1

    2024年02月12日
    浏览(45)
  • LeetCode //C - 153. Find Minimum in Rotated Sorted Array

    Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: [4,5,6,7,0,1,2] if it was rotated 4 times. [0,1,2,4,5,6,7] if it was rotated 7 times. Notice that rotating an array [a[0], a[1], a[2], …, a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], …, a[n

    2024年02月06日
    浏览(35)
  • Algorithms practice:leetcode 33. Search in Rotated Sorted Array

    Algorithms practice:leetcode33 Search in Rotated Sorted Array There is an integer array ,nums , sorted in ascending order (with distinct values). Prior to being passed to your function, nums is possibly rotated,at an unknown pivot index k (1 = k nums.length) such that the resulting array is [nums[k], nums[k+1], …, nums[n-1], nums[0], nums[1], …, nums

    2024年01月21日
    浏览(48)
  • LeetCode //C - 19. Remove Nth Node From End of List

    Given the head of a linked list, remove the n t h n^{th} n t h node from the end of the list and return its head.   Example 1: Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5] Example 2: Input: head = [1], n = 1 Output: [] Example 3: Input: head = [1,2], n = 1 Output: [1] Constraints: The number of nodes in the list is sz. 1 = sz = 30 0 = Node.val = 100 1

    2024年02月10日
    浏览(37)
  • 【每日一题】Leetcode - 19. Remove Nth Node From End of List

    Leetcode - 19. Remove Nth Node From End of List Drawing on the method of finding midpoints in linked lists, use quick slow pointer Finding midpoints in linked lists nothing

    2024年02月12日
    浏览(50)
  • LeetCode80. 删除有序数组中的重复项 II(JavaScript版)

    LeetCode题目链接 题目描述:给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使得出现次数超过两次的元素只出现两次 ,返回删除后数组的新长度。 不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。 js版本

    2024年02月06日
    浏览(34)
  • leetcode做题笔记80删除有序数组中的重复项 II

    给你一个有序数组  nums  ,请你  原地  删除重复出现的元素,使得出现次数超过两次的元素 只出现两次  ,返回删除后数组的新长度。 不要使用额外的数组空间,你必须在  原地 修改输入数组  并在使用 O(1) 额外空间的条件下完成。 说明: 为什么返回数值是整数,但输

    2024年02月12日
    浏览(50)
  • 算法leetcode|80. 删除有序数组中的重复项 II(rust重拳出击)

    给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使得出现次数超过两次的元素只出现两次 ,返回删除后数组的新长度。 不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。 说明 : 为什么返回数值是整数,但输出的答案是

    2024年02月09日
    浏览(50)
  • 80. 删除有序数组中的重复项 II

    题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台  解题思路:因为数组有序,相等的元素一定相邻,所以可以使用一个变量num统计相等元素的个数,如果当前元素和前一个元素相等,令num++。另外使用一个变量len记录删除后数组的新长度,如果当前正在遍历的元

    2024年02月13日
    浏览(45)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包