Open azl397985856 opened 1 year ago
双指针,重拳出击
function removeDuplicates(nums: number[]): number {
let i = 1, cur = nums[0], j = 0
while (i < nums.length) {
if (nums[i] !== cur) {
nums[j + 1] = nums[i]
j++
cur = nums[i]
}
i++
}
return nums.length - i + j + 1
};
双指针法
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
p = 0
for i in range(1, len(nums)):
if nums[i] == nums[i-1]:
continue
else:
p += 1
nums[p] = nums[i]
# 题目要求返回的是长度,所有坐标要加 1
return p + 1
复杂度分析
用快慢指针(读写指针)
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
left=right=0
if not nums:
return 0
while right<len(nums):
if nums[right]!=nums[left]:#如果不相等,则读指针后移,写指针也后
left+=1
nums[left]=nums[right]#保证最后返回的nums不重复
right+=1
return left+1#索引从0开始
复杂度分析
双指针: 指针left指向数组的第一个元素 指针right指向数组的第二个元素
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {
if(nums.length < 1) return 1;
let left = 0, right = 0;
while(right < nums.length) {
if(nums[left] == nums[right]) {
right++;
} else {
nums[left + 1] = nums[right];
left++;
right++;
}
}
return left + 1;
};
复杂度分析
class Solution:
# nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
def removeDuplicates(self, nums):
if not nums:
return 0
i = 0
for j in range(1, len(nums)):
if nums[j] != nums[i]:
i += 1
nums[i] = nums[j]
return i + 1
solution = Solution()
nums1 = [1, 1, 2]
length1 = solution.removeDuplicates(nums1)
print("Modified Array 1:", nums1[:length1])
nums2 = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]
length2 = solution.removeDuplicates(nums2)
print("Modified Array 2:", nums2[:length2])
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int cnt=1;
for(int i=0;i<nums.size()-1;i++){
if(nums[i+1]!=nums[i]) cnt++;
}
int s=1;
for(int i=1;i<nums.size();i++){
if(nums[i]!=nums[i-1]) {
nums[s]=nums[i];
s++;
}
}
return cnt;
}
};
Double pointers
class Solution {
public int removeDuplicates(int[] nums) {
int slow = 0, fast = slow+1;
while (fast < nums.length) {
while (fast < nums.length && nums[fast] == nums[slow]) {
fast++;
}
if (fast < nums.length) {
nums[++slow] = nums[fast];
}
}
return slow+1;
}
}
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
n = len(nums)
j = 0
for i in range(n):
if nums[i] != nums[j]:
j += 1
nums[j] = nums[i]
return j + 1
/*
思路:
双指针
slow,fast
复杂度: 时间复杂度: O(n) 空间复杂度: O(1)
*/
func removeDuplicates(nums []int) int {
if len(nums) == 0 {
return 0
}
slow, fast := 0, 0
for fast < len(nums) {
if nums[slow] == nums[fast] {
fast++
continue
}
nums[slow+1] = nums[fast]
fast++
slow++
}
// nums = nums[:slow+1]
return slow + 1
}
function removeDuplicates(nums: number[]): number {
if (nums == null || nums.length == 0) return 0;
let p = 0;
let q = 1;
while (q < nums.length) {
if (nums[p] != nums[q]) {
nums[p + 1] = nums[q];
p++;
}
q++;
}
return p + 1;
};
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function(nums) {
if(nums.length == 0) {
return 0;
}
let slow = 0;
let fast = 0;
while(fast < nums.length) {
if(nums[fast] != nums[slow]) {
slow++
nums[slow] = nums[fast]
}
fast++
}
return slow + 1
};
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
slow, fast = 0, 1
while fast < len(nums):
if nums[fast] != nums[slow]:
slow = slow + 1
# 维护 nums[0..slow] 无重复
nums[slow] = nums[fast]
fast = fast + 1
return slow + 1
class Solution {
public int removeDuplicates(int[] nums) {
// 双指针
int i = 0,j = 1;
for(; j < nums.length;){
if(nums[i] == nums[j]){
j++;
} else {
nums[++i] = nums[j++];
}
}
return i+1;
}
}
class Solution {
public int removeDuplicates(int[] nums) {
// 双指针,
int n = nums.length;
int fast = 1;
int slow = 1;
while (fast < n) {
if (nums[fast -1] != nums[fast]) {
nums[slow] = nums[fast];
slow++;
}
fast++;
}
return slow ;
}
}
function removeDuplicates(nums: number[]): number { let i = 1, cur = nums[0], j = 0 while (i < nums.length) { if (nums[i] !== cur) { nums[j + 1] = nums[i] j++ cur = nums[i] } i++ } return nums.length - i + j + 1 };
暴力和双子针写法
/**
* @param {number[]} nums
* @return {number}
*/
var removeDuplicates = function (nums) {
let len = nums.length;
if (len === 0 || len === 1) return nums.length;
if (len === 2 && nums[0] !== nums[1]) return 2;
let left = 0;
let right = 1;
while (right < len) {
const letVal = nums[left];
const rightVal = nums[right];
if (letVal === rightVal) {
nums.splice(right, 1);
len--
} else {
left++;
right++;
}
}
return nums.length;
};
var removeDuplicates = function (nums) {
const len = nums.length;
if (len === 0 || len === 1) return nums.length;
if (len === 2 && nums[0] !== nums[1]) return 2;
let left = 0;
let right = 1;
while (right < len) {
const letVal = nums[left];
const rightVal = nums[right];
if (letVal === rightVal) {
right++
} else {
left++
nums[left] = rightVal;
}
}
return left + 1;
};
class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length <= 1) {
return nums.length;
}
int i = 0;
int j = 1;
while (j < nums.length) {
if (nums[i] != nums[j]) {
++i;
nums[i] = nums[j];
}
++j;
}
return i + 1;
}
}
26.删除排序数组中的重复项
入选理由
暂无
题目地址
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/
前置知识
暂无
题目描述