当前位置:网站首页>26. 删除有序数组中的重复项 C#解答
26. 删除有序数组中的重复项 C#解答
2022-07-04 17:39:00 【charlsdm】
给你一个 升序排列 的数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。元素的 相对顺序 应该保持 一致 。
由于在某些语言中不能改变数组的长度,所以必须将结果放在数组nums的第一部分。更规范地说,如果在删除重复项之后有 k 个元素,那么 nums 的前 k 个元素应该保存最终结果。
将最终结果插入 nums 的前 k 个位置后返回 k 。
不要使用额外的空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。
判题标准:
系统会用下面的代码来测试你的题解:
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 。不需要考虑数组中超出新长度后面的元素。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/remove-duplicates-from-sorted-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
public class Solution
{
public int RemoveDuplicates(int[] nums)
{
int i = 0;
for(int j=1;j<nums.Length;j++)
{
if(nums[i]!=nums[j])
{
nums[i + 1] = nums[j];
i = i + 1;
}
}
return i+1;
}
}
边栏推荐
- Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers
- C语言打印练习
- 2022 ByteDance daily practice experience (Tiktok)
- SSL证书续费相关问题详解
- [cloud voice suggestion collection] cloud store renewal and upgrading: provide effective suggestions, win a large number of code beans, Huawei AI speaker 2!
- 模板_大整数减法_无论大小关系
- Halcon template matching
- Scala basic tutorial -- 15 -- recursion
- [uniapp] uniapp development app online Preview PDF file
- Don't just learn Oracle and MySQL!
猜你喜欢
Improve the accuracy of 3D reconstruction of complex scenes | segmentation of UAV Remote Sensing Images Based on paddleseg
2022 ByteDance daily practice experience (Tiktok)
Journal des problèmes de brosse à boutons de force / day6 / 6.28
Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers
Microservice architecture debate between radical technologists vs Project conservatives
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
整理混乱的头文件,我用include what you use
Li Kou brush question diary /day8/7.1
Process of manually encrypt the mass-producing firmware and programming ESP devices
Scala基础教程--13--函数进阶
随机推荐
2022 ByteDance daily practice experience (Tiktok)
《看完就懂系列》字符串截取方法substr() 、 slice() 和 substring()之间的区别和用法
2022-07-04: what is the output of the following go language code? A:true; B:false; C: Compilation error. package main import 'fmt' func
vbs或vbe如何修改图标
Li Kou brush question diary /day3/2022.6.25
2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import 'fmt' func
【2022年江西省研究生数学建模】水汽过饱和的核化除霾 思路分析及代码实现
How is the entered query SQL statement executed?
[opencv introduction to mastery 9] opencv video capture, image and video conversion
Perfect JS event delegation
C语言打印练习
Scala基础教程--15--递归
Microservice architecture debate between radical technologists vs Project conservatives
Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii
[go language question brushing chapter] go conclusion chapter | introduction to functions, structures, interfaces, and errors
IBM WebSphere MQ检索邮件
力扣刷题日记/day2/2022.6.24
Li Kou brush question diary /day7/6.30
6.26cf simulation race e: solution to the problem of price maximization
Esp32-c3 introductory tutorial questions ⑫ - undefined reference to ROM_ temp_ to_ power, in function phy_ get_ romfunc_ addr