当前位置:网站首页>Remove duplicates from an ordered array
Remove duplicates from an ordered array
2022-06-13 00:56:00 【-LM-】
Remove duplicate items from an ordered array
Method 1 ( Two pointer thought )
Use cnt To record the subscript of the new array ,t To record every value added to the new array . First, you need to traverse the original array , The original array was ordered , Therefore, when the number is different from the previous one, it needs to be added to the new array , Then update t. Now t As the latest number to be compared , If the following sum t inequality , Then add it to the new array . The new array here is not redeclared , Instead, it replaces the original array ,cnt It is used to represent the subscript of the array updated each time , from 0 Start .
public static int removeDuplicates(int[] nums) {
if(nums.length==0) return 0;
int len = nums.length;
int cnt=0;
int t=nums[0];
for(int i=0;i<len;i++){
if(nums[i]!=t){
cnt++;
nums[cnt] = nums[i];
t = nums[i];
}
}
return cnt+1;
}
Method 2 ( Double pointer )
Official explanation
Define two pointers \textit{fast}fast and \textit{slow}slow Fast pointer and slow pointer respectively , The fast pointer indicates the subscript position reached by traversing the array , The slow pointer indicates the subscript position to be filled in by the next different element , Initially, both pointers point to the subscript 11.
class Solution {
public int removeDuplicates(int[] nums) {
int n = nums.length;
if (n == 0) {
return 0;
}
int fast = 1, slow = 1;
while (fast < n) {
if (nums[fast] != nums[fast - 1]) {
nums[slow] = nums[fast];
++slow;
}
++fast;
}
return slow;
}
}
边栏推荐
- Binary tree - right view
- Get preview of precast body
- Sequence table - find main element
- Androi天气
- [virtual machine] notes on virtual machine environment problems
- Today's sleep quality record 74 points
- [003] embedded learning: creating project templates - using stm32cubemx
- Cve-2021-24078 vulnerability analysis
- Aunt learning code sequel: ability to sling a large number of programmers
- Undirected graph -- computing the degree of a node in compressed storage
猜你喜欢

Kotlin 协程挂起函数 suspend 关键字

Opencv desaturation

Physical orbit simulation

. The way to prove the effect of throwing exceptions on performance in. Net core

DNS attack surface analysis

Common skills of quantitative investment -- Drawing Part 1: Drawing stock closing price curve and ochl candle chart

Three column simple Typecho theme lanstar/ Blue Star Typecho theme

Common skills of quantitative investment - index part 2: detailed explanation of BOL (Bollinger line) index, its code implementation and drawing

深度学习每周期的步数多少合适?

What is the difference between pytorch and tensorflow?
随机推荐
Common skills of quantitative investment - index part 2: detailed explanation of BOL (Bollinger line) index, its code implementation and drawing
The grass is bearing seeds
MySQL query table field information
Common skills for quantitative investment - drawing 3: drawing the golden section line
[JS component] floating text
Unitywebrequest asynchronous Download
[JS] solve the problem that removeeventlistener is invalid after the class listening event from new is bound to this
Arduino control soil moisture sensor
五篇经典好文,值得一看
Get preview of precast body
高阶极点对于波形的影响
Opencv desaturation
ImportError: cannot import name &#039; get_ ora_ doc&#039; from partially initialized module
How to solve the duplication problem when MySQL inserts data in batches?
What is the difference between pytorch and tensorflow?
Et5.0 simply transform referencecollectorieditor
3623. Merge two ordered arrays
Hard (magnetic) disk (I)
. The way to prove the effect of throwing exceptions on performance in. Net core
Oceanbase is the leader in the magic quadrant of China's database in 2021