This is the second day of my participation in the August More Text Challenge
describe
Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. 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]; }Copy the code
If all assertions pass, then your solution will be accepted.
Example 1:
Input: nums = [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. It does not matter what you leave beyond the returned k (hence they are underscores).Copy the code
Example 2:
Input: nums =,0,1,1,1,2,2,3,3,4 [0] 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. It does not matter what you leave beyond the returned k (hence they are underscores).Copy the code
Note:
0 <= nums.length <= 3 * 10^4
-100 <= nums[i] <= 100
nums is sorted in non-decreasing order.
Copy the code
parsing
The number of elements left in the numS list is returned by removing all duplicate elements from the numS list and ensuring that the relative positions of the elements do not change, and requiring that no additional space be applied. If the current element is not equal to nums[index], incrementing the index by one. Nums [index] indicates that different elements can be stored in the index position. At the end of the loop, simply return index+1 to indicate the number of elements.
answer
class Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ if len(nums)==0:return 0 index = 0 for i in range(1, len(nums)): if nums[i]! =nums[index]: index += 1 nums[index] = nums[i] return index+1Copy the code
The results
Given nodes in the Python online submission list. Node Usage: Given nodes in Python online submissions for Remove Duplicates from Sorted Array.Copy the code
parsing
In addition, because the duplicate numbers are next to each other, if the count record is used to remove the duplicate number from left to right, the corresponding index of the second non-duplicate number will be moved to the left by count, so the solution is to traverse nums from the second element. If nums[I] is the same as nums[i-1], count + 1, if nums[I] is different, nums[i-1] can be assigned to nums[i-count]. Use len(nums)-count to calculate the number of digits that are not repeated.
answer
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == 0: return 0
count = 0
for i in range(1,len(nums)):
if nums[i]==nums[i-1]:
count += 1
else:
nums[i-count] = nums[i]
return len(nums)-count
Copy the code
The results
Given nodes in the Python online submissions with removed Duplicates from Sorted Array. Given nodes in Python online submissions for Remove Duplicates from Sorted Array.Copy the code
Original link: leetcode.com/problems/re…
Your support is my biggest motivation