当前位置:网站首页>26. Delete the duplicate item C solution in the ordered array
26. Delete the duplicate item C solution in the ordered array
2022-07-04 19:20:00 【charlsdm】
To give you one Ascending order Array of nums , Would you please In situ Delete duplicate elements , Make each element Only once , Returns the new length of the deleted array . Elemental Relative order It should be maintained Agreement .
Because the length of an array cannot be changed in some languages , So you have to put the result in an array nums The first part of . More formally , If there is... After deleting duplicates k Elements , that nums Before k An element should hold the final result .
Insert the final result into nums Before k Return to... After a position k .
Don't use extra space , You must be there. In situ Modify input array And using O(1) Complete with extra space .
Criteria for judging questions :
The system will use the following code to test your solution :
int[] nums = […]; // Input array
int[] expectedNums = […]; // The expected answer with the correct length
int k = removeDuplicates(nums); // call
assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
assert nums[i] == expectedNums[i];
}
If all assertions pass , Then your solution will be adopt .
Example 1:
Input :nums = [1,1,2]
Output :2, nums = [1,2,_]
explain : Function should return the new length 2 , And the original array nums The first two elements of are modified to 1, 2 . You don't need to think about the elements in the array that follow the new length .
Example 2:
Input :nums = [0,0,1,1,1,2,2,3,3,4]
Output :5, nums = [0,1,2,3,4]
explain : Function should return the new length 5 , And the original array nums The first five elements of are modified to 0, 1, 2, 3, 4 . You don't need to think about the elements in the array that follow the new length .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/remove-duplicates-from-sorted-array
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
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;
}
}
边栏推荐
- Scala基础教程--13--函数进阶
- 2022CoCa: Contrastive Captioners are Image-Text Fountion Models
- IBM WebSphere MQ retrieving messages
- 使用FTP
- Uni app and uviewui realize the imitation of Xiaomi mall app (with source code)
- Scala basic tutorial -- 17 -- Collection
- 从实时应用角度谈通信总线仲裁机制和网络流控
- BI技巧丨权限轴
- 1672. 最富有客户的资产总量
- Caché WebSocket
猜你喜欢
![[发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0](/img/4e/4154fec22035725d6c7aecd3371b05.jpg)
[发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0

node_exporter部署

Scala基础教程--20--Akka

使用canal配合rocketmq监听mysql的binlog日志

Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology

My colleagues quietly told me that flying Book notification can still play like this

The latest progress of Intel Integrated Optoelectronics Research promotes the progress of CO packaging optics and optical interconnection technology

从实时应用角度谈通信总线仲裁机制和网络流控

自由小兵儿

【2022年江西省研究生数学建模】冰壶运动 思路分析及代码实现
随机推荐
Nature Microbiology | 可感染阿斯加德古菌的六种深海沉积物中的病毒基因组
[go ~ 0 to 1] read, write and create files on the sixth day
sqlserver的CDC第一次查询的能读取到数据,但后面增删改读取不到,是什么原因
LeetCode第300场周赛(20220703)
【uniapp】uniapp开发app在线预览pdf文件
更安全、更智能、更精致,长安Lumin完虐宏光MINI EV?
6.26cf simulation race e: solution to the problem of price maximization
利用策略模式优化if代码【策略模式】
Perfect JS event delegation
2021 合肥市信息学竞赛小学组
Principle and application of ThreadLocal
Scala basic tutorial -- 15 -- recursion
Scala基础教程--15--递归
基于C语言的菜鸟驿站管理系统
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
Download the first Tencent technology open day course essence!
Go微服务(二)——Protobuf详细入门
What if the self incrementing ID of online MySQL is exhausted?
神经网络物联网是什么意思通俗的解释
基于lex和yacc的词法分析器+语法分析器