LeetCode practise

LeetCode刷题笔记

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

给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。

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

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int removeDuplicates(int[] nums) {
int n=nums.length;
int head=1;
if(n==0){
return 0;
}
else{
for(int i=1;i<n;i++){
if(nums[i]!=nums[i-1]){
nums[head]=nums[i];
head++;
}
}
}
return head;
}
}

题目26来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/perfect-number


LeetCode practise
http://example.com/2021/12/26/LeetCode/
Author
lzdong
Posted on
December 26, 2021
Licensed under