当前位置:网站首页>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;
}
};
边栏推荐
- 【LeetCode】53. Maximum subarray and
- 同花顺股票账户开户安全吗
- LNMP platform docking redis service
- 【890. 查找和替换模式】
- 【Proteus仿真】简易数码管定时器时钟
- Flutter series part: detailed explanation of GridView layout commonly used in flutter
- [image denoising] image denoising based on trilateral filter with matlab code
- JVM Basics - > how GC determines that an object can be recycled
- Anti aliasing / anti aliasing Technology
- Use js to listen for Keydown event
猜你喜欢
![[Part 8] semaphore source code analysis and application details [key points]](/img/e2/05c08435d60564aaa1172d2d574675.jpg)
[Part 8] semaphore source code analysis and application details [key points]

数字藏品的发展趋势!

Alcohol detector based on 51 single chip microcomputer

The shutter library recommends sizer to help you easily create a responsive UI
![[C language] data type occupation](/img/12/e0f9679076d89fb5bd993ee3c345bf.jpg)
[C language] data type occupation

设计消息队列存储消息数据的 MySQL 表格

C language: how to give an alias to a global variable?

年薪50万是一条线,年薪100万又是一条线…...

Photoshop:ps how to enlarge a picture without blurring

iShot
随机推荐
iShot
生成小程序菊花码(生成菊花码、更换中间logo、更改图片尺寸,加文字)
[890. find and replace mode]
Es6+ new content
Is it safe to open an account with new bonds? How should novices operate?
IPhone: save Boolean into core data - iphone: save Boolean into core data
【Proteus仿真】简易数码管定时器时钟
What you must know about cloud computing
vim利用右下4键
Unity commonly used 3D mathematical calculation
認識的幾比特清華同學都離職了……
打新债开户安全么,新手该怎么操作?
【LeetCode】5. Longest Palindromic Substring
Flutter库推荐Sizer 可帮助您轻松创建响应式 UI
Zabbix的功能介绍和常用术语
Is it safe to open an account in flush? How to open an account online to buy stocks
Research Report on water sports shoes industry - market status analysis and development prospect forecast
JVM Basics - > What are the thread shared areas in the JVM
Use group_ Dplyr issues when using group_ by(multiple variables)
ShardingSphere-proxy-5.0.0部署之分表实现(一)