Open azl397985856 opened 2 years ago
思路 顺序找过去,有不一样的就 index +1,然后顺手把原位置的数字改了
代码
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
res = 1
for i in range(1, len(nums)):
if nums[i] != nums[i-1]:
nums[res] = nums[i]
res += 1
return res
复杂度 时间 O(n) 空间 O(1)
思路: 双指针的用法,这道题有道变形题,删除排序数组的重复项II,区别就是与l-k进行判断 思路:顺序遍历,当l<K时,直接左移。当l=k时,开始判断。 如果当前值与nums[l-k]相等,说明数组已经存满当前值,跳过。 否则就赋值加右移
func removeDuplicates(nums []int) int {
l,k := 0,1
for r := range nums{
if l< k||nums[r] != nums[l-k]{
nums[l] = nums[r]
l++
}
}
return l
}
时间复杂度O(N) 空间复杂度O(1)
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.size()<2){
return nums.size();
}
int i=0,j=1;
for (; j < nums.size();) {
if(nums[i]==nums[j]){
nums.erase(nums.begin()+j);
continue;
}
++i,++j;
}
return nums.size();
}
};
双指针,类似快慢
class Solution {
public int removeDuplicates(int[] nums) {
int quick=0;
int slow=0;
while(quick<nums.length)
{
nums[slow]=nums[quick];
quick+=1;
while (quick<nums.length && nums[quick]==nums[quick-1])
{
quick+=1;
}
slow+=1;
}
return slow;
}
}
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function (nums) {
let i = 0;
let j = 1;
while (j < nums.length) {
if (nums[i] === nums[j]) {
j++
} else {
nums[i + 1] = nums[j];
i++;
j++;
}
}
return i + 1
};
复杂度分析不是很会,不一定对,如果有错,请指正。
不要搞骚操作 能删就删 为什么要倒腾
def removeDuplicates(self, nums: List[int]) -> int:
fast,slow=1,0
while fast<len(nums):
if nums[fast]==nums[slow]:
while nums[fast]==nums[slow]:
fast+=1
if fast>len(nums)-1:
return slow+1
nums[slow+1]=nums[fast]
fast+=1
slow+=1
return slow+1
2 pointers
class Solution {
public int removeDuplicates(int[] nums) {
int s = 0, f = 0;
while(f < nums.length){
if(nums[s] != nums[f]){
s++;
nums[s] = nums[f];
}
f++;
}
return s + 1;
}
}
复杂度分析
快慢指针,用 slow 指针保证 [0, slow] 这一段不重复
class Solution {
public int removeDuplicates(int[] nums) {
int slow = 0;
for (int fast = 1; fast < nums.length; fast++) {
if (nums[slow] != nums[fast]) {
nums[++slow] = nums[fast];
}
}
return slow + 1;
}
}
时间复杂度:O(n) 空间复杂度: O(1)
//s6
//s6
class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length <= 1) return nums.length;
int writer = 0;
for(int reader = 1;reader < nums.length;reader++){
if(nums[writer] != nums[reader]){
writer++;
nums[writer] = nums[reader];
}
}
return writer+1;
}
}
//读写双指针 time: O(N) space: O(1)
Day26 26
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int n = nums.size();
if(n == 0 || n == 1) return n;
int fast = 1, slow = 1;
while(fast < n){
if(nums[fast] != nums[fast - 1]){
nums[slow] = nums[fast];
++slow;
}
++fast;
}
return slow;
}
};
Complexity
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
x = 0
while x< len(nums)-1:
if nums[x]<nums[x+1]:
x +=1
else:
nums.pop(x)
return len(nums)
思路
代码
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if not nums:
return 0
l, r = 0, 0
while r < len(nums):
if nums[l] != nums[r]:
l += 1
nums[l] = nums[r]
r += 1
return l+1
复杂度分析
class Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ if len(nums) <= 1: return 1
prev = 0
for i in range(1, len(nums)):
if nums[prev] != nums[i]:
prev += 1
nums[prev] = nums[i]
return prev + 1
双指针
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if not nums:
return 0
start=0
length =len(nums)
for end in range(1,length):
if nums[start] != nums[end]:
start+=1
nums[start] = nums[end]
return start+1
复杂度分析
难度: 简单
给你一个有序数组 nums
,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。
不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。
说明:
为什么返回数值是整数,但输出的答案是数组呢?
请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
你可以想象内部操作如下:
// nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
int len = removeDuplicates(nums);
// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
for (int i = 0; i < len; i++) {
print(nums[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 。不需要考虑数组中超出新长度后面的元素。
提示:
0 <= nums.length <= 3 * 104
-104 <= nums[i] <= 104
nums
已按升序排列快慢指针:快指针永远在慢指针前一位,如果两个指针所指向数字相同则删除快指针的元素直到结尾
后文有较好的做法
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
// trivial cases
if(nums.size() <= 1) return nums.size();
int fast = 1,slow = 0;//快慢指针定义
while(fast < nums.size())
{
while(nums[slow] == nums[fast] && fast < nums.size())
{
nums.erase(nums.begin() + fast);
}
slow++;
fast++;
}
return nums.size();
}
};
注意点:
- 快慢指针方法
while
也要加入判断条件防止越界erase()
中需要传入迭代器,可以通过nums.begin() + i
的方式实现目的
vector
的 erase
函数时间复杂度不是 $O(1)$class Solution {
public:
int removeDuplicates(vector<int>& nums) {
//trivial cases
if(nums.size() <= 1) return nums.size();
int l = 0, r = 1;
while(r<nums.size())
{
while(r < nums.size() && nums[r] == nums[l]) r++;
if(r >= nums.size()) break;
else
{
nums[l+1] = nums[r];
l++;
}
}
return l+1;
}
};
nums.size()
class Solution { public int removeDuplicates(int[] nums) { int i = 0, j = 1;
while (j < nums.length) {
if (nums[j] != nums[j - 1]) {
i++;
nums[i] = nums[j];
}
j++;
}
return i + 1;
}
}
# two pointers
# target_index increment by 1 when nums[target_index] != nums[i]
# updates nums[target_index] = nums[i]
# time: O(N)
# space: O(1)
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if len(nums) == 0:
return 0
target_index = 0
for i, num in enumerate(nums):
if num != nums[target_index]:
target_index += 1
nums[target_index] = num
return target_index + 1
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
for i in nums[:]:
if nums.count(i) > 1:
nums.remove(i)
return len(nums)
class Solution:
# 双指针:一个cnt留在原地,一个快指针依次遍历,cnt仅在发现不同时移动并赋值
def removeDuplicates(self, nums: List[int]) -> int:
cnt = 0
for num in nums:
if nums[cnt] != num:
cnt += 1
nums[cnt] = num
return cnt + 1
计数
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
k=1
for i in range(1,len(nums)):
if nums[i] != nums[i-1]:
nums[k]=nums[i]
k=k+1
return k
C++ Code:
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
// two pointer.
// Space O(n) Space O(1)
if(nums.size()==0)
return 0;
int count =0;
for(int i=1; i< nums.size(); i++)
{
if(nums[i]!=nums[count])
{
count++;
nums[count] = nums[i];
}
}
return count+1;
}
};
快慢指针
var removeDuplicates = function(nums) {
if(!nums.length) return 0
let i = 0
for(let j = 0; j < nums.length; j++) {
if(nums[i] !== nums[j]){
i++
nums[i] = nums[j]
}
}
return i + 1
};
左右指针
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
// if(nums == nullptr)return -1;
int len = nums.size();
if(len == 0) return 0;
int left = 0;
int right = 0;
while(right<len) {
//最终满足返回要求的数据收集,而非处理重复数据
if(nums[left]!= nums[right]) {
nums[++left] = nums[right];
}
right++;
}
return left+1;
}
};
复杂度分析
Two pointers. Pointer P points to the previous element. Move the other pointer Q forward till it points to the first element not equals to the element P points to. Update P by one, and replace the element it points to to the element pointer Q currently at.
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if len(nums) < 2:
return len(nums)
ans = 1
pre = 0
for i in range(1, len(nums)):
if nums[i] == nums[pre]:
continue
pre += 1
nums[pre] = nums[i]
ans += 1
return ans
Time: O(N) Space: O(1)
读写指针想象成两个独立的数组。写指针指向最后写入的数据。 读指针的值和写指针不相等时,需要写入新数据。
class Solution {
public int removeDuplicates(int[] nums) {
int write = 0;
for (int read = 1; read < nums.length; read++) {
if (nums[read] != nums[write]) {
nums[++write] = nums[read];
}
}
return write + 1;
}
}
TC: O(N) SC: O(1)
# 从第一个开始比较,比较其与下一个元素,如果相等,则把这个元素pop出列表
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
Length = len(nums)
# if Length == 0:
# return 0
i = 0
for count in range(Length-1):
if nums[i+1] == nums[i]:
nums.pop(i)
else:
i = i + 1
return len(nums)
class Solution {
public int removeDuplicates(int[] nums) {
int left = 0, right = 0;
while (right < nums.length) {
if (left == right) {
right++;
} else if (nums[left] == nums[right]) {
right++;
} else {
left++;
nums[left] = nums[right];
right++;
}
}
return left + 1;
}
}
Time: O(N) Space: O(1)
Code:
public class Solution { public int RemoveDuplicates(int[] nums) { if (nums == null || nums.Length == 0) return 0;
int count = 0;
for (int i = 0; i < nums.Length; i++)
{
if (nums[count] != nums[i] && count <= i)
{
count++;
nums[count] = nums[i];
}
}
return count + 1;
}
}
有序、数组、删除重复元素、O(1)额外空间
->双指针读写指针
思路
有序数组,重复元素连续分布,删除元素后,相邻元素值不相等;
读写指针;
步骤
class Solution {
public int removeDuplicates(int[] nums) {
if(nums == null){
return 0;
}
int read = 0,write = 0;
while(read < nums.length){
if(!(nums[write] == nums[read])){
nums[++write] = nums[read];
}
read++;
}
return write+1;
}
}
时间:O(n)
空间:O(1)
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if len(nums) == 0:
return 0
target_index = 0
for i, num in enumerate(nums):
if num != nums[target_index]:
target_index += 1
nums[target_index] = num
return target_index + 1
思路: 双指针.
func removeDuplicates(nums []int) int {
if len(nums) == 0 {
return 0
}
slow :=1
for fast :=1 ; fast < len(nums) ;fast ++ {
if nums[fast] != nums[fast - 1]{
nums[slow] = nums[fast]
slow ++
}
}
return slow
}
时间复杂度:n 空间复杂度:1
快慢指针。slow 指针初始化为0,fast指针初始化为1。fast指针一直往右走,如果 slow 指针指向的值不等于 fast,则将 fast 位置的值赋给 slow + 1 位置。最终返回 slow +1
class Solution {
public int removeDuplicates(int[] nums) {
// 思路:快慢指针
int slow = 0;
for (int fast = 1; fast < nums.length; fast++) {
if (nums[fast] != nums[slow]) {
nums[++slow] = nums[fast];
}
}
return slow + 1;
}
}
利用读指针和写指针。读的如果和上一个数字不同就写进写指针的位置,写指针移动一位。读指针每次移动一位,每次操作完之后读记录一下指针位置的数字。
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
read, write = 0, 0
last_read = None
while read < len(nums):
if nums[read] != last_read:
nums[write] = nums[read]
write += 1
last_read = nums[read]
read += 1
return write
TC: O(N) SC: O(1)
双指针原地替换数组元素
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == 0:
return 0
left, right = 0, 0
while right < len(nums) - 1:
right += 1
if nums[right] != nums[left]:
left += 1
nums[left] = nums[right]
return left + 1
复杂度分析
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
l = r = 1
n = len(nums)
while r < n:
while r < n and nums[r] == nums[r-1]:
r += 1
if r < n:
nums[l] = nums[r]
l += 1
r += 1
return l
使用快慢指针,快指针遍历数组,慢指针指向待修改的位置。
var removeDuplicates = function(nums) {
let i = 0;
let k = 1;
for(let num of nums){
if(i < k || nums[i - k] !== num){
nums[i] = num;
i++;
}
};
nums.splice(i);
return i;
};
复杂度分析
code:
public static int removeDuplicates(int[] nums) {
if (nums == null || nums.length == 0) return 0;
int count = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[i - 1] != nums[i]) {
nums[count++] = nums[i];
}
}
return count;
}
var removeDuplicates = function(nums) {
let index = 0
for(let i = 0; i < nums.length; i++) {
if(nums[i] !== nums[i + 1]) {
nums[index] = nums[i]
index++
}
}
return index
};
var removeDuplicates = function (nums) {
if (!nums.length) return 0;
let i = 0;
for (let j = 1; j < nums.length; j++) {
if (nums[j] !== nums[i]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
};
双指针
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if not nums:
return 0
n = len(nums)
fast = slow = 1
# 将快指针一次遍历从1到n-1的每一个位置
while fast < n:
# 如果nums[fast] != nums[fast - 1], 说明nums[fast]和之前的元素都不同
if nums[fast] != nums[fast - 1]:
# 将nums[fast]的值复制到nums[slow]中,
nums[slow] = nums[fast]
# slow 指向下一个值
slow += 1
fast += 1
# 遍历完, num[0] 到nums[slow - 1]的每个元素都是不相同的
return slow
使用双指针
class Solution { public int removeDuplicates(int[] nums) { //使用双 指针 int size = nums.length; if(size==0)return 0; int i=0,j=0; for(j=i+1;j<size;j++){ if(nums[j]!=nums[i]){ if(j!=i+1){ int temp =nums[i+1]; nums[i+1]=nums[j]; nums[j]=temp; } i++; } } return i+1; } }
时间复杂度:O(n)
空间复杂度:O(1)
双指针
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if not nums:
return 0
index = 0
for i, num in enumerate(nums):
if num > nums[index]:
index += 1
nums[index] = num
return index + 1
思路
快慢指针,遍历一遍数组。
代码
class Solution {
public int removeDuplicates(int[] nums) {
int length = nums.length;
int left = 0;
int right = 1;
while(left < length && right < length){
if(nums[left] != nums[right]){
nums[left + 1] = nums[right];
left++;
right++;
}
else{
right++;
}
}
return left + 1;
}
}
复杂度
时间复杂度:O(n)
空间复杂度:O(1)
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int k = 0;
if (nums.empty()) return 0;
for (int i = 1; i < nums.size(); i++) {
if (nums[k] != nums[i]) nums[++k] = nums[i];
}
return k + 1;
}
};
class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length <= 1) {
return nums.length;
}
int curr = 1;
int fill = 1;
while (curr < nums.length) {
if (nums[curr] != nums[fill - 1]) {
nums[fill] = nums[curr];
fill++;
curr++;
} else {
curr++;
}
}
return fill;
}
}
time: O(n) space: O(1)
Algo
- DNA
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if not nums: return 0
slow = quick = 0
while quick < len(nums):
if nums[slow] != nums[quick]:
slow += 1
nums[slow] = nums[quick]
quick += 1
return slow + 1
使用双指针,slow 指针指向已经整理好的数组的最后一个元素后面。fast 指针用于扫描数组,初始值等于 slow。
用一个变量 pre 记住已经整理好的数组的最后一个值。pre 的初始值是 nums[0],slow 和 fast 的初始值是 1。
fast 向前遍历,如果发现等于 pre 的值,就跳过,发现不等于 pre 的值,就把它交换到 slow 的位置,更新 pre,然后slow 和 fast 前移。
时间复杂度 O(n),空间复杂度 O(1)
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
slow = 1
fast = 1
n = len(nums)
if n == 0: return 0
pre = nums[0]
while fast < n:
if nums[fast] != pre:
nums[slow] = nums[fast]
pre = nums[slow]
slow += 1
fast += 1
return slow
快慢指针
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.size() == 0)
return 0;
int first = 0, last = 0;
int duplicate = nums[0];
for(last; last < nums.size(); ++last)
{
if(duplicate != nums[last])
{
nums[first] = duplicate;
first++;
duplicate = nums[last];
}
}
if(nums[first] != nums[last-1])
nums[first] = duplicate;
return first+1;
}
};
class Solution(object):
def removeDuplicates(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
r = w = 0
while r < len(nums):
if nums[r] != nums[w]:
w += 1
nums[w] = nums[r]
r += 1
return w+1
时间复杂度:O(N)
空间复杂度:O(1)
采用双指针,因为原数组是有序的 需要一个指针往右边走 找到和前一个不一样的 左指针就把对应的数组位置进行修改
class Solution {
public int removeDuplicates(int[] nums) {
if (nums == null) {
return 0;
}
if (nums.length == 1) {
return 1;
}
//第一个数 不用管 所以索引从1开始
int left = 1, right = 1;
while (right < nums.length) {
//如果和前面的不一样
if (nums[right] != nums[right - 1]) {
//就把原数组的对应的位置改变
nums[left] = nums[right];
left++;
}
right++;
}
return left;
}
}
复杂度分析 时间复杂度: O(n) 空间复杂度:O(1)
26.删除排序数组中的重复项
入选理由
暂无
题目地址
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
前置知识
暂无
题目描述