# 算法养成记：删除排序数组中的重复项

LeetCode26

Remove Duplicates from Sorted Array

Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

Given nums = [1,1,2], Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the returned length.

Example 2:

Given nums = [0,0,1,1,1,2,2,3,3,4], Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively. It doesn't matter what values are set beyond the returned length.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

Internally you can think of this:

// nums is passed in by reference. (i.e., without making a copy) int len = removeDuplicates(nums); // any modification to nums in your function would be known by the caller. // using the length returned by your function, it prints the first len elements. for (int i = 0; i < len; i++) { print(nums[i]); }

// nums 是以“引用”方式传递的。也就是说，不对实参做任何拷贝

int len = removeDuplicates(nums);

// 在函数里修改输入数组对于调用者是可见的。

// 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。

for (int i = 0; i < len; i++) {

print(nums[i]);

}

0 条评论

• ### 算法养成记：移除元素

Given an array nums and a value val, remove all instances of that value in-place...

• ### 我是怎么读源码的，授之以渔

首先我们要对我们的目标有所了解，知道她有什么特点，有些什么功能。对对方都还不了解，就想着进入别人的内心世界，那不是臭不要脸嘛，我们要做一个有着流氓心的绅士；对她...

• ### 算法养成记：最后字符长度

Given a string s consists of upper/lower-case alphabets and empty space characte...

• ### 【leetcode系列】26. 删除排序数组中的重复项

https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/description...

• ### 星际转移轨道设计的差分进化优化工具（CS AI）

星际转移轨道设计中极其敏感和高度非线性的搜索空间给全局优化带来了巨大挑战。作为代表，目前已知的由欧洲航天局(ESA)设计的全球轨道优化问题(GTOP)的最佳解是...

• ### CodeForces 157A Game Outcome

A. Game Outcome time limit per test 2 seconds memory limit per test 256 me...

• ### 数据仓库专题（23）：总线矩阵的另类应用-Drill Down into a More Detailed Bus Matrix

Many of you are already familiar with the data warehouse bus architecture and m...

• ### POJ-1276-Cash Machine（多重背包）

Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions:...

• ### FZU 2150 Fire Game（BFS）

Problem 2150 Fire Game Accept: 1302    Submit: 4569 Time Limit: 1000 mSec    M...