当前位置:网站首页>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;
}
};
边栏推荐
- 常见渲染管线整理
- JVM foundation > CMS garbage collector
- Qt Quick 3D学习:使用鼠标键盘控制节点位置和方向
- 【LeetCode】数组中第K大的元素
- 四元数简介
- Is it safe to open an account in tonghuashun? How to open an account for securities
- Plusieurs camarades de classe de Tsinghua sont partis...
- web3 原则和去中心化
- USB机械键盘改蓝牙键盘
- One article to quickly understand whether there are security risks in your network
猜你喜欢

JVM foundation - > three ⾊ mark

Redis optimization

The interface testing tool apipos3.0 is applicable to process testing and reference parameter variables

SQL query list all views in SQL Server 2005 database - SQL query to list all views in an SQL Server 2005 database

基于51单片机的酒精检测仪
![[C language] data type occupation](/img/12/e0f9679076d89fb5bd993ee3c345bf.jpg)
[C language] data type occupation

Database daily question --- day 10: combine two tables

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

【图像去噪】基于三边滤波器实现图像去噪附matlab代码

Shardingsphere-proxy-5.0.0 deployment table implementation (I)
随机推荐
2022-02-28 incluxdb high availability planning
IPhone: save Boolean into core data - iphone: save Boolean into core data
【LeetCode】300.最长上升子序列
【LeetCode】5. 最长回文子串
【数据分析】基于 kmeans实现数据聚类分组含Matlab源码
C语言:如何给全局变量起一个别名?
[probability theory and mathematical statistics] final review: formula summary and simple examples (end)
JVM foundation - > talk about class loader two parent delegation model
【LeetCode】5. Longest Palindromic Substring
MOOG servo valve d634-341c/r40ko2m0nss2
QT quick 3D learning: mouse picking up objects
MySQL case when then function use
Why is pain rating important?
China Aquatic Fitness equipment market trend report, technical innovation and market forecast
LNMP platform docking redis service
Research Report on truffle fungus industry - market status analysis and development prospect forecast
Research Report on market supply and demand and strategy of China's digital camera lens industry
[C language] data type occupation
Zabbix的功能介绍和常用术语
微信小程序提现功能