当前位置:网站首页>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;
}
}
边栏推荐
- File processing examples of fopen, FREAD, fwrite, fseek
- Torchdrug tutorial
- 使用SSH
- ByteDance dev better technology salon was successfully held, and we joined hands with Huatai to share our experience in improving the efficiency of web research and development
- One question per day (2022-07-02) - Minimum refueling times
- Scala basic tutorial -- 14 -- implicit conversion
- php伪原创api对接方法
- Scala基础教程--14--隐式转换
- Nebula Importer 数据导入实践
- Scala基础教程--13--函数进阶
猜你喜欢

自由小兵儿

力扣刷题日记/day7/2022.6.29

A method of using tree LSTM reinforcement learning for connection sequence selection

【2022年江西省研究生数学建模】冰壶运动 思路分析及代码实现

Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
![[mathematical modeling of graduate students in Jiangxi Province in 2022] analysis and code implementation of haze removal by nucleation of water vapor supersaturation](/img/da/d46cca19f34223d29003be2e33aaa4.png)
[mathematical modeling of graduate students in Jiangxi Province in 2022] analysis and code implementation of haze removal by nucleation of water vapor supersaturation

VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题

Digital "new" operation and maintenance of energy industry

Li Kou brush question diary /day3/2022.6.25
redis分布式锁的8大坑总结梳理
随机推荐
工厂从自动化到数字孪生,图扑能干什么?
A method of using tree LSTM reinforcement learning for connection sequence selection
Don't just learn Oracle and MySQL!
神经网络物联网是什么意思通俗的解释
自由小兵儿
Li Kou brush question diary /day3/2022.6.25
Scala基础教程--17--集合
redis分布式锁的8大坑总结梳理
物联网应用技术的就业前景和现状
未来几年中,软件测试的几大趋势是什么?
[mathematical modeling of graduate students in Jiangxi Province in 2022] analysis and code implementation of haze removal by nucleation of water vapor supersaturation
IBM WebSphere MQ检索邮件
Scala basic tutorial -- 18 -- set (2)
Scala基础教程--19--Actor
Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers
启牛开的证券账户安全吗?
Nature microbiology | viral genomes in six deep-sea sediments that can infect Archaea asgardii
力扣刷題日記/day6/6.28
1、 Introduction to C language
2022健康展,北京健博会,中国健康展,大健康展11月13日