当前位置:网站首页>Delete the duplicate items in the ordered array -- force deduction question 26 (simple)
Delete the duplicate items in the ordered array -- force deduction question 26 (simple)
2022-06-12 06:24:00 【Fishbone CC】
Remove duplicate items from an ordered array – Force to buckle 26 topic ( Simple )
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 .
At first I thought of using HashMap, Simple but time-consuming 2ms( Only more than 8% Of java Submit user )
public int removeDuplicates(int[] nums) {
HashMap<Integer,Object> map = new HashMap<>();
int index = 0;
for (int i = 0; i < nums.length; i++) {
if (!map.containsKey(nums[i])){
map.put(nums[i],null);
nums[index] = nums[i];
index ++;
}
}
return index;
}
After some improvement, I thought of double pointer , Time consuming 1ms exceed 44% Of java Submit user
public int removeDuplicates(int[] nums) {
int j = 0;
for(int i = 0;i<nums.length;i++){
if(i ==0 || nums[i] != nums[i-1]){
nums[j] = nums[i];
j++;
}
}
return j;
}
Then I found that my double pointer was not standard ...
Here are the official answers , Time consuming 0ms exceed 100% Of java Submit user
public int removeDuplicates(int[] nums) {
int n = nums.length;
if (n == 0){
return 0;
}
int fast = 1,slow = 1;
for (int i = 1; i < n; i++) {
if (nums[fast] != nums[fast-1]){
nums[slow] = nums[fast];
slow++;
}
fast++;
}
return slow;
}
I made a dynamic diagram of the execution process according to the code 
We still need the official answer !!!
边栏推荐
- Project progress on February 28, 2022
- Cv2.fillpoly coco annotator segment coordinate conversion to mask image
- English grammar_ Adverb_ With or without ly, the meaning is different
- Multithreading (IV) -- no lock (IV) -- unsafe
- leetcode 300. Longest increasing subsequence
- (UE4 4.27) customize primitivecomponent
- Qt-- realize TCP communication
- Storing texture2d to hard disk JPG file with script under unity3d
- (UE4 4.27) add globalshder to the plug-in
- Analysis of memory management mechanism of (UE4 4.26) UE4 uobject
猜你喜欢

Explanation of sensor flicker/banding phenomenon

Multithreading (4) -- no lock (3) -- longadder source code

Cause analysis of motion blur / smear caused by camera shooting moving objects

相机图像质量概述

RMB classification II

LeetCode个人题解(剑指offer3-5)3.数组中重复的数字,4.二维数组中的查找,5.替换空格

Bulk Rename Utility

Redis configuration (III) -- master-slave replication

SQL 注入读写文件

n次贝塞尔曲线
随机推荐
GET 和 POST 的区别及留言板代码实现
LeetCode-1587. Bank account summary II
JS变量作用域
Video based fire smoke detection using robust AdaBoost
Multithreading (V) -- Concurrent tools (II) -- j.u.c concurrent contracting (I) -- AQS and reentrantlock principles
Leetcode January 12 daily question 334 Increasing ternary subsequence
(UE4 4.27) customize primitivecomponent
LeetCode-1741. Find total time spent per employee
Zip and Items() difference
PHP 读写 COOKIE
勤于奋寻找联盟程序方法介绍
Getting started with houdininengine HDA and UE4
LeetCode-1405. Longest happy string
LeetCode-1154. Day of the year
Video fire detection based on Gaussian mixture model and multi-color
Chartextcnn (Ag dataset - news topic classification)
Simple spiral ladder generation for Houdini program modeling
Unity surface shader with template buffer
LeetCode个人题解(剑指offer3-5)3.数组中重复的数字,4.二维数组中的查找,5.替换空格
Simulateur nightGod ADB View log