简体   繁体   English

从排序数组中删除重复项 - 我不明白为什么我的解决方案在某些情况下不起作用

[英]Remove Duplicates from Sorted Array - I don't understand why my solution doesn't work in certain situation

I'm trying to solve a Leetcode question "Remove Duplicates from Sorted Array" .我正在尝试解决 Leetcode 的问题"Remove Duplicates from Sorted Array" But my solution only works on certain situation.但我的解决方案只适用于特定情况。 I've tried to debug it looking through the result on Quokka.js.我试图通过 Quokka.js 上的结果来调试它。 But I can't still understand "WHY" I get the weird result on myArr2.但我仍然无法理解“为什么”我在 myArr2 上得到了奇怪的结果。 Anyone can help a stupid person??任何人都可以帮助一个愚蠢的人?? Thx in advance.提前谢谢。

My solution is我的解决方案是

 let myArr = [0, 0, 1, 1, 2, 2]; let myArr2 = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]; const removedDuplicates = (nums) => { let k; for (let i = 0; i < nums.length; i++) { console.log("nums: ", nums); console.log(nums.length); // console.log("I'm I: ",i); if (nums[i] == nums[i + 1]) { nums.splice(nums[i + 1], 1); console.log("after ", i + 1, " iteration nums: ", nums); } } k = nums.length; return k; }; console.log(removedDuplicates(myArr)); // [0,1,2] console.log(removedDuplicates(myArr2)); // [0,1,2,3,3,4]... Why is "3" still in the array??

Below is the problem or you can check the problem here以下是问题,或者您可以在此处检查问题

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once.给定一个按非降序排序的 integer 数组 nums,就地删除重复项,使每个唯一元素只出现一次。 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.由于在某些语言中无法更改数组的长度,因此您必须将结果放在数组 nums 的第一部分。 More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result.更正式地说,如果删除重复项后有 k 个元素,则 nums 的前 k 个元素应该保存最终结果。 It does not matter what you leave beyond the first k elements.在前 k 个元素之外留下什么并不重要。

Return k after placing the final result in the first k slots of nums.将最终结果放入 nums 的前 k 个槽后返回 k。

Do not allocate extra space for another array.不要为另一个数组分配额外的空间。 You must do this by modifying the input array in-place with O(1) extra memory.您必须通过使用 O(1) 额外的 memory 就地修改输入数组来做到这一点。

Custom Judge:自定义裁判:

The judge will test your solution with the following code:法官将使用以下代码测试您的解决方案:

int[] nums = [...]; int[] 数字 = [...]; // Input array int[] expectedNums = [...]; // 输入数组 int[] expectedNums = [...]; // The expected answer with correct length // 正确长度的预期答案

int k = removeDuplicates(nums); int k = removeDuplicates(nums); // Calls your implementation // 调用你的实现

assert k == expectedNums.length;断言 k == expectedNums.length; for (int i = 0; i < k; i++) { assert nums[i] == expectedNums[i]; for (int i = 0; i < k; i++) { 断言 nums[i] == expectedNums[i]; } If all assertions pass, then your solution will be accepted.如果所有断言都通过,那么您的解决方案将被接受。

Example 1:示例 1:

Input: nums = [1,1,2] Output: 2, nums = [1,2,_] Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.输入:nums = [1,1,2] Output: 2, nums = [1,2,_] 解释:你的 function 应该返回 k = 2,其中 nums 的前两个元素分别为 1 和 2。 It does not matter what you leave beyond the returned k (hence they are underscores).在返回的 k 之外留下什么并不重要(因此它们是下划线)。 Example 2:示例 2:

Input: nums = [0,0,1,1,1,2,2,3,3,4] Output: 5, nums = [0,1,2,3,4, , , , ,_] Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.输入: nums = [0,0,1,1,1,2,2,3,3,4] Output: 5, nums = [0,1,2,3,4, , , , ,_] 解释:您的 function 应返回 k = 5,nums 的前五个元素分别为 0、1、2、3 和 4。 It does not matter what you leave beyond the returned k (hence they are underscores).在返回的 k 之外留下什么并不重要(因此它们是下划线)。

Constraints:约束:

0 <= nums.length <= 3 * 104 -100 <= nums[i] <= 100 nums is sorted in non-decreasing order. 0 <= nums.length <= 3 * 104 -100 <= nums[i] <= 100 nums 按非递减顺序排序。

This is the case of Value Type and Reference Type.这是值类型和引用类型的情况。 You are trying to remove the number from nums array which also affect the original array.您正在尝试从 nums 数组中删除数字,这也会影响原始数组。 Here's my code in Python you can check:这是我在 Python 中的代码,您可以查看:

def removeDuplicates(self, nums: List[int]) -> int:
        expectedItem = nums[:]
        myarr = []
        for i in range(len(expectedItem)):
            if expectedItem[i] not in myarr :
                myarr.append(expectedItem[i])
            else:
                nums.remove(expectedItem[i])
        
        return len(nums);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不明白为什么我的setInterval + jQuery不起作用 - Don't understand why my setInterval + jQuery doesn't work 我的for循环不起作用,我也不知道为什么 - my for loop doesn't work and I don't know why 我的 innerHTML 不起作用,我不知道为什么? - My innerHTML doesn't work and I don't know why? 我不知道为什么我的 backgroundImage 不起作用 - I don't how why my backgroundImage doesn't work Vue 3 路由器不工作。 我不明白为什么 - Vue 3 router doesn't work. I don't understand why JS - 我不明白为什么这个对象声明不起作用 - JS - I don't understand why this declaration of object doesn't work 我的数组中没有任何数据,只有一个,就是这样,我不明白为什么? - My array isn't taking any data in it, only one and that's it, and I don't understand why? 我不明白这种情况,有人可以向我解释一下吗? - I don't understand this situation, can someone explain this to me? 我不明白,为什么我的 CalculateTotal () function 返回“Null”? - I don't understand, Why is my CalculateTotal () function returning "Null"? Javascript计算排列-当我不复制数组时,为什么我的代码返回不需要的解决方案? - Javascript computing permutations - why is my code returning unwanted solution when I don't make a copy of the array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM