2-数组-22-删除有序数组中的重复项-LeetCode26
LeetCode:题目序号26
更多内容欢迎关注我(持续更新中,欢迎Star✨)
Github:CodeZeng1998/Java-Developer-Work-Note
技术公众号:CodeZeng1998(纯纯技术文)
生活公众号:好锅(Life is more than code)
CSDN: CodeZeng1998
其他平台:CodeZeng1998、好锅
给你一个 非严格递增排列 的数组 nums
,请你** 原地** 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。元素的 相对顺序 应该保持 一致 。然后返回 nums
中唯一元素的个数。
考虑 nums
的唯一元素的数量为 k
,你需要做以下事情确保你的题解可以被通过:
- 更改数组
nums
,使nums
的前k
个元素包含唯一元素,并按照它们最初在nums
中出现的顺序排列。nums
的其余元素与nums
的大小不重要。 - 返回
k
。
判题标准:
系统会用下面的代码来测试你的题解:
int[] nums = [...]; // 输入数组
int[] expectedNums = [...]; // 长度正确的期望答案
int k = removeDuplicates(nums); // 调用
assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
assert nums[i] == expectedNums[i];
}
如果所有断言都通过,那么您的题解将被 通过。
示例 1:
输入:nums = [1,1,2]
输出:2, nums = [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]
解释:函数应该返回新的长度 5 , 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4 。不需要考虑数组中超出新长度后面的元素。
提示:
1 <= nums.length <= 3 * 104
-104 <= nums[i] <= 104
nums
已按 非严格递增 排列
/**
* 删除有序数组中的重复项(快慢指针)
*
* @param nums 待处理的数组
* @return 返回处理后的数组的长度
*/
public int removeDuplicates(int[] nums) {
if (nums.length == 0) {
return 0;
}
int slowIndex = 1;
for (int fastIndex = 1; fastIndex < nums.length; fastIndex++) {
if (nums[fastIndex] != nums[fastIndex - 1]) {
nums[slowIndex] = nums[fastIndex];
slowIndex++;
}
}
return slowIndex;
}
ChatGPT:
这段代码是一个名为 removeDuplicates
的方法,它接受一个整数数组 nums
作为输入参数。其目的是移除数组中的重复元素,并返回移除重复元素后剩余元素的数量。
下面是代码的解释:
- 空数组检查:
if (nums.length == 0) { return 0; }
:如果输入数组的长度为0,则直接返回0,因为空数组中没有重复元素。
- 初始化慢指针:
int slowIndex = 1;
:这行代码初始化了一个慢指针slowIndex
,它从索引位置1开始,因为第一个元素无论如何都是不重复的。
- 双指针迭代:
for (int fastIndex = 1; fastIndex < nums.length; fastIndex++) {
:这个循环使用两个指针来遍历数组。快指针fastIndex
从索引位置1开始,逐步增加。- 循环内部:if (nums[fastIndex] != nums[fastIndex - 1]) {:这个条件检查当前元素与前一个元素是否相同。
- 如果当前元素与前一个元素不相同,表示当前元素是一个新的不重复元素,需要保留。
nums[slowIndex] = nums[fastIndex];
:将当前不重复的元素复制到慢指针slowIndex
所指示的位置。slowIndex++;
:将慢指针向后移动一位,以便指向下一个位置,准备接收下一个不重复元素。
- 如果当前元素与前一个元素不相同,表示当前元素是一个新的不重复元素,需要保留。
- 返回剩余元素的数量:
return slowIndex;
:循环结束后,慢指针slowIndex
指向的位置之前的所有元素都是不重复的。因此,slowIndex
的值表示数组中剩余不重复元素的数量。将这个值作为方法的结果返回。
总的来说,这个方法使用了两个指针来遍历数组,通过逐步移动慢指针来覆盖重复的元素,从而实现了移除重复元素的目的。最后,返回的结果是剩余不重复元素的数量。
26. Remove Duplicates from Sorted Array
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. Then return the number of unique elements in nums
.
Consider the number of unique elements of nums
to be k
, to get accepted, you need to do the following things:
- Change the array
nums
such that the firstk
elements ofnums
contain the unique elements in the order they were present innums
initially. The remaining elements ofnums
are not important as well as the size ofnums
. - Return
k
.
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,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).
Example 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.
It does not matter what you leave beyond the returned k (hence they are underscores).
Constraints:
1 <= nums.length <= 3 * 104
-100 <= nums[i] <= 100
nums
is sorted in non-decreasing order.
更多内容欢迎关注我(持续更新中,欢迎Star✨)
Github:CodeZeng1998/Java-Developer-Work-Note
技术公众号:CodeZeng1998(纯纯技术文)
生活公众号:好锅(Life is more than code)
CSDN: CodeZeng1998
其他平台:CodeZeng1998、好锅