当前位置:网站首页>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;
}
}
边栏推荐
- [mathematical basis of machine learning] (I) linear algebra (Part 1 +)
- 基于lex和yacc的词法分析器+语法分析器
- 6.26CF模拟赛B:数组缩减题解
- Scala basic tutorial -- 17 -- Collection
- 工厂从自动化到数字孪生,图扑能干什么?
- Wanghongru research group of Institute of genomics, Chinese Academy of Agricultural Sciences is cordially invited to join
- 力扣刷题日记/day4/6.26
- Process of manually encrypt the mass-producing firmware and programming ESP devices
- Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
- 基于C语言的菜鸟驿站管理系统
猜你喜欢
激进技术派 vs 项目保守派的微服务架构之争
. Net ORM framework hisql practice - Chapter 2 - using hisql to realize menu management (add, delete, modify and check)
Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers
Build your own website (15)
vbs或vbe如何修改图标
What if the self incrementing ID of online MySQL is exhausted?
Li Kou brush question diary /day1/2022.6.23
An example of multi module collaboration based on NCF
力扣刷题日记/day1/2022.6.23
Don't just learn Oracle and MySQL!
随机推荐
输入的查询SQL语句,是如何执行的?
Esp32-c3 introductory tutorial questions ⑫ - undefined reference to ROM_ temp_ to_ power, in function phy_ get_ romfunc_ addr
大佬们,求助一下,我用mysql cdc 2.2.1(flink 1.14.5)写入kafka,设置
[发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0
Li Kou brush question diary /day7/2022.6.29
一、C语言入门基础
Scala basic tutorial -- 14 -- implicit conversion
Angry bird design based on unity
Caché JSON 使用JSON适配器
Using FTP
正则替换【JS,正则表达式】
From automation to digital twins, what can Tupo do?
力扣刷题日记/day7/2022.6.29
Pb extended DLL development (super chapter) (VII)
【2022年江西省研究生数学建模】水汽过饱和的核化除霾 思路分析及代码实现
Interpretation of SIGMOD '22 hiengine paper
[uniapp] uniapp development app online Preview PDF file
其他InterSystems %Net工具
模板_大整数减法_无论大小关系
Wireshark抓包TLS协议栏显示版本不一致问题