当前位置:网站首页>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;
}
}
边栏推荐
- Improve the accuracy of 3D reconstruction of complex scenes | segmentation of UAV Remote Sensing Images Based on paddleseg
- From automation to digital twins, what can Tupo do?
- Lex and yacc based lexical analyzer + parser
- 基于C语言的菜鸟驿站管理系统
- Mxnet implementation of googlenet (parallel connection network)
- 模板_大整数减法_无论大小关系
- 6.26cf simulation match B: solution to array reduction problem
- How to open an account is safe,
- Perfect JS event delegation
- Scala basic tutorial -- 12 -- Reading and writing data
猜你喜欢
千万不要只学 Oracle、MySQL!
ThreadLocal原理与使用
Use canal and rocketmq to listen to MySQL binlog logs
Learning path PHP -- phpstudy "hosts file does not exist or is blocked from opening" when creating the project
Scala basic tutorial -- 18 -- set (2)
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
Li Kou brush question diary /day4/6.26
[go ~ 0 to 1] read, write and create files on the sixth day
Scala基础教程--19--Actor
英特尔集成光电研究最新进展推动共封装光学和光互连技术进步
随机推荐
sqlserver的CDC第一次查询的能读取到数据,但后面增删改读取不到,是什么原因
神经网络物联网应用技术就业前景【欢迎补充】
英特尔集成光电研究最新进展推动共封装光学和光互连技术进步
Li Chi's work and life summary in June 2022
Learning path PHP -- phpstudy "hosts file does not exist or is blocked from opening" when creating the project
6.26cf simulation race e: solution to the problem of price maximization
Caché JSON 使用JSON适配器
Use canal and rocketmq to listen to MySQL binlog logs
整理混乱的头文件,我用include what you use
[mathematical modeling of graduate students in Jiangxi Province in 2022] analysis and code implementation of haze removal by nucleation of water vapor supersaturation
Microservice architecture debate between radical technologists vs Project conservatives
Scala基础教程--18--集合(二)
基于unity的愤怒的小鸟设计
Using FTP
C语言打印练习
技术分享 | 接口测试价值与体系
Send and receive IBM WebSphere MQ messages
Perfect JS event delegation
Scala基础教程--14--隐式转换
Cache é JSON uses JSON adapters