当前位置:网站首页>LeetCode —— 26. Remove duplicates from an ordered array
LeetCode —— 26. Remove duplicates from an ordered array
2022-06-12 22:34:00 【Listen to the sea】
subject
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 .
Example
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 .
Ideas
Double pointer algorithm . First a pointer points to the first position , The second pointer starts from the next pointer , The number pointed to is compared with the number pointed to by the pointer , If equal , No operation , Continue to traverse ; If not equal , Move the pointer back and assign the unequal number to the number pointed to by the current pointer . And so on .
Code
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int k = 0;
for (int i = 1; i < nums.size(); i ++ ) {
if (nums[k] != nums[i]) nums[ ++ k] = nums[i];
}
return k + 1;
}
};
边栏推荐
- Database daily question --- day 10: combine two tables
- Hostvars in ansible
- [machine learning] learning notes 01- introduction
- Yyds dry goods inventory solution Huawei machine test: weighing weight
- JVM foundation > GC generation: minorgc majorgc fullgc mixed GC
- VIM use the lower right 4 keys
- China's elastic belt market trend report, technical dynamic innovation and market forecast
- 【LeetCode】53. Maximum subarray and
- 【LeetCode】300. Longest ascending subsequence
- iShot
猜你喜欢
The annual salary of 500000 is one line, and the annual salary of 1million is another line
JVM Basics - > What are the thread shared areas in the JVM
Anti aliasing / anti aliasing Technology
Coordinate transformation in pipelines
The development trend of digital collections!
Flutter库推荐Sizer 可帮助您轻松创建响应式 UI
flutter系列之:flutter中常用的GridView layout详解
Audio and video technology development weekly 𞓜 234
【Web技术】1348- 聊聊水印实现的几种方式
[C language] data type occupation
随机推荐
Why is pain rating important?
China embolic coil market trend report, technical innovation and market forecast
3.5 测试类的setup和teardown
Qrcodejs2 QR code generation JS
JVM foundation - > three ⾊ mark
在同花顺开户证券安全吗,买股票怎么网上开户
ShardingSphere-proxy-5.0.0部署之分表实现(一)
JVM Basics - > What are the thread shared areas in the JVM
同花顺股票账户开户安全吗
IPhone: save Boolean into core data - iphone: save Boolean into core data
证券开户有风险吗?怎么开户安全呢?
be careful! Your Navicat may have been poisoned
Use group_ Dplyr issues when using group_ by(multiple variables)
Pat grade A - 1167 Cartesian tree (30 points) (buildtree + level traversal)
How to perform disaster recovery and recovery for kubernetes cluster? (22)
One article to quickly understand whether there are security risks in your network
JVM foundation - > what is STW?
The annual salary of 500000 is one line, and the annual salary of 1million is another line
C # reading table data in word
【LeetCode】209. Minimum length subarray