Leetcode No.26 Remove Duplicates from Sorted Array(c++实现)
1. 题目
1.1 英文题目
Given an integer array nums sorted in non-decreasing order, remove the duplicates in-placein-place such that each unique element appears only once. The relative order of the elements should be kept the same.
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.
Return k after placing the final result in the first k slots of nums.
Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
1.2 中文题目
给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。
1.3输入输出
| 输入 | 输出 |
|---|---|
| nums = [1,1,2] | 2, nums = [1,2,_] |
| [0,0,1,1,1,2,2,3,3,4] | 5, nums = [0,1,2,3,4,,,,,_] |
2. 实验平台
IDE:VS2019
IDE版本:16.10.1
语言:c++11
3. 程序
3.1 测试程序
#include "Solution.h"
#include <vector> // std::vector
#include<iostream> // std::cout
using namespace std;
// 主程序
void main()
{
vector<int> nums = { 0,0,0,1,1,1,2,2,3,4,4 }; // 输入
Solution solution; // 实例化Solution
int k = solution.removeDuplicates(nums); // 主功能
// 输出
cout << k << ", [";
for (int i = 0; i < k; i++)
{
if (i == k - 1)
cout << nums[i] << "]";
else
cout << nums[i] << ",";
}
}
3.2 功能程序
3.2.1 最佳程序
(1)代码
#pragma once
#include<vector> // std::vector
using namespace std;
//主功能
class Solution {
public:
int removeDuplicates(vector<int>& nums)
{
if (nums.size() > 1)
{
int i; // i为慢指针,指向的是去重后最后一位,随j更新
int j; // j为快指针,不断向前探索
for (i = 0, j = 0; j < nums.size(); j++)
{
if (nums[i] != nums[j]) // 若快指针所指元素与慢指针相同,则慢的不动,快的前进一步
nums[++i] = nums[j]; // 若不同,则慢的前进一步指向快指针指的元素,快指针继续向前一步
}
return i + 1; // 因为下标从0开始,所以去重后的元素个数为i+1
}
}
};
此程序参考:https://blog.csdn.net/qjh5606/article/details/81434396
(2)解读
快慢指针,慢的指向的一直都是新的,也就是说来一个新的它才往前走一步,快的就是不断向前,发现和慢的不一样的就汇报给那个慢的,然后慢的更新。如果二者相等,则慢的不动,快的前进一步;如果二者不等,则慢的前进一步的同时更新那个新一步指向的数字为现在快指针指向的新数字,快的同时也往前走一步。
解读参考:https://www.cnblogs.com/forPrometheus-jun/p/10889152.html
3.2.2 自写程序
(1)代码
#pragma once
#include<vector> // std::vector
#include<algorithm> // std::find
using namespace std;
//主功能
class Solution {
public:
int removeDuplicates(vector<int>& nums)
{
if (nums.size() > 1) // 若nums不是空集合或单元素集合
{
int i = 1;
while (i != nums.size())
{
if (nums[i] == nums[i - 1]) // 若元素重复
nums.erase(find(nums.begin(), nums.end(), nums[i])); //则去除重复数字
else // 若元素不重复
i += 1; // 则向后再查看一个元素
}
}
return nums.size(); // 返回结果
}
};
(2)思路
从前往后遍历,如果发现有元素重复,则将重复元素剔除
3.2.3 其他程序
(1)代码
#pragma once
#include<vector> // std::vector
#include<algorithm> // std::unique
using namespace std;
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
nums.erase(std::unique(nums.begin(), nums.end()), nums.end());
return nums.size();
}
};
(2)解读
该程序直接使用 c++标准模板库STL中的函数std::unique查找重复元素,更简洁。
4. 相关知识
(1) 快慢指针(Fast-Slow Pointer)
这几个教程挺不错的,可以参考一下:
https://www.cnblogs.com/hxsyl/p/4395794.html
https://blog.csdn.net/SoftPoeter/article/details/103153564
https://zhuanlan.zhihu.com/p/361049436
(2) i++与++i的区别
- i++ 即后加加,原理是:先自增,然后返回自增之前的值
- ++i 即前加加,原理是:先自增,然后返回自增之后的值
参考:https://blog.csdn.net/android_cai_niao/article/details/106027313
Leetcode No.26 Remove Duplicates from Sorted Array(c++实现)的更多相关文章
- [Leetcode][Python]26: Remove Duplicates from Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...
- C# 写 LeetCode easy #26 Remove Duplicates from Sorted Array
26.Remove Duplicates from Sorted Array Given a sorted array nums, remove the duplicates in-place suc ...
- 【leetcode】 26. Remove Duplicates from Sorted Array
@requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...
- 【一天一道LeetCode】#26. Remove Duplicates from Sorted Array
一天一道LeetCode系列 (一)题目 Given a sorted array, remove the duplicates in place such that each element app ...
- 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...
- 【LeetCode】26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- LeetCode OJ 26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- LeetCode:26. Remove Duplicates from Sorted Array(Easy)
1. 原题链接 https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ 2. 题目要求 给定一个已 ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
随机推荐
- MYSQL导入/迁移后事件不执行
mysql迁移后事件不执行 查看数据库是否开启事件支持 mysql> show variables like 'event_scheduler'; +-----------------+---- ...
- grasshopper DataTree 树形数据以及Graft Flatten Simplify的理解
问题的来源: 要在grasshopper里面输出 类似于二维数组的数据 但是在 grasshopper里的 C# 电池里面,无法显示 二维ArrayList里面的数据. 在 C# 电池里参考这个帖子: ...
- 08.ElementUI 2.X 源码学习:源码剖析之工程化(三)
0x.00 前言 项目工程化系列文章链接如下,推荐按照顺序阅读文章 . 1️⃣ 源码剖析之工程化(一):项目概览.package.json.npm script 2️⃣ 源码剖析之工程化(二):项目构 ...
- 大型图像数据聚类匹配:ICCV2019论文解析
大型图像数据聚类匹配:ICCV2019论文解析 Jointly Aligning Millions of Images with Deep Penalised Reconstruction Conge ...
- iOS视频硬编码技术
iOS视频硬编码技术 一.iOS视频采集硬编码 基本原理 硬编码 & 软编码 硬编码:通过系统自带的Camera录制视频,实际上调用的是底层的高清编码硬件模块,即显卡,不使用CPU,速度快 软 ...
- 使用IDEA创建Maven项目
一.创建一个普通的Maven项目 1.启动IDEA 2.创建一个Maven项目 3.Maven的目录结构 二.使用模板创建一个MavenWeb项目 1.启动IDEA 2.创建一个MavenWeb项目 ...
- 【Azure 应用服务】Azure Web App的服务(基于Windows 操作系统部署)在被安全漏洞扫描时发现了TCP timestamps漏洞
问题背景 什么是TCP timestamps(TCP 时间戳)? The remote host implements TCP Timestamps, as defined by RFC1323 (h ...
- 阿里云视频云 Retina 多媒体 AI 体验馆开张啦!
带你体验视频更多可能 海量视频管理难度大?翻库检索特定人物费时费力?视频内容剪辑效率低?您的得力助手"Retina多媒体AI"体验馆已上线.带你感受视频AI黑科技,开启极致智能体验 ...
- 【NX二次开发】不健全的双击按钮。
为什么说不健全,是因为 双击按钮时会先运行单击事件,这个后面再解决.但是模仿某公司的图层操作工具是没有问题了,因为这个工具运行双击事件时本来就需要运行单击事件,不仔细看容易被唬住. 图层操作工具(双击 ...
- 07:mysql的unknown variable ‘xxxxx’
简单说明一下: 可能有的找不到配置文件的,不要慌,这个时候 你可能以前安装了多个版本的mysql 就是说你以前是mysql5,现在换成了mysql8, 矮!! 你可能发现你的mysql8里面没有配置文 ...