Leetcode No.27 Remove Element(c++实现)
1. 题目
1.1 英文题目
Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The relative order of the elements may be changed.
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 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。
不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。
元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。
1.3输入输出
| 输入 | 输出 |
|---|---|
| nums = [3,2,2,3], val = 3 | 2, nums = [2,2,,] |
| nums = [0,1,2,2,3,0,4,2], val = 2 | 5, nums = [0,1,4,0,3,,,_] |
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,1,2,2,3,0,4,2 };
int val = 2;
Solution solution; // 实例化Solution
int k = solution.removeElement(nums, val); // 主功能
// 输出
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 removeElement(vector<int>& nums, int val)
{
int res = 0; // 存储去重后数组元素个数
for (int i = 0; i < nums.size(); ++i) // 快指针i遍历数组
if (nums[i] != val) // 若快指针指向元素不是目标元素
nums[res++] = nums[i]; // 则慢指针res+1,且慢指针指向快指针当前元素
return res;
}
};
此程序参考:https://www.cnblogs.com/lightwindy/p/8628297.html
(2)解读
快慢指针对于这种O(1)空间复杂度的数组问题真的很好用,虽然菜鸡的我还没有掌握它的灵魂,废话不多说。这个方法用的快慢指针法,也叫双指针法。res作为慢指针,始终指向的是去重后的数组元素,而i作为快指针,一马当先,遍历整个数组。如果快指针指向的元素不是目标元素,也就是去重元素,就让慢指针+1,同时指向该元素。i遍历一遍,算法结束。
解读参考:https://www.cnblogs.com/forPrometheus-jun/p/10889152.html
3.2.2 自写程序
(1)代码
#pragma once
#include<vector> // std::vector
using namespace std;
//主功能
class Solution
{
public:
int removeElement(vector<int>& nums, int val)
{
while(1)
{
vector<int>::iterator iter = find(nums.begin(), nums.end(), val); // 在数组中找到目标元素
if (iter != nums.end()) // 若找到
nums.erase(iter); // 则删除该元素
else // 若找不到,即数组中不存在该目标元素,则不再查找
break;
}
return nums.size();
}
};
(2)思路
菜鸡的我只想到暴力算法,遍历整个数组,遇到去重元素,就删除该元素。遍历完后,返回去重后数组长度即可。
从前往后遍历,如果发现有元素重复,则将重复元素剔除
4. 相关知识
(1) 快慢指针(Fast-Slow Pointer)
关于快慢指针,Leetcode的26题也用到了该算法,这个算法真不错,26题的该算法解法可以参考:https://www.cnblogs.com/yunmeng-shi/p/14927735.html
Leetcode No.27 Remove Element(c++实现)的更多相关文章
- [Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
- C# 写 LeetCode easy #27 Remove Element
27. Remove Element Given an array nums and a value val, remove all instances of that value in-place ...
- 【LeetCode】27. Remove Element (2 solutions)
Remove Element Given an array and a value, remove all instances of that value in place and return th ...
- 【LeetCode】27 - Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- 【一天一道LeetCode】#27. Remove Element
一天一道LeetCode系列 (一)题目 Given an array and a value, remove all instances of that value in place and ret ...
- 【LeetCode】27. Remove Element 解题报告(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 记录起始位置 日期 题目地址:https:/ ...
- LeetCode OJ 27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. D ...
- LeetCode:27. Remove Element(Easy)
1. 原题链接 https://leetcode.com/problems/remove-element/description/ 2. 题目要求 给定一个整数数组 nums[ ] 和一个整数 val ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- 工作流引擎详解!工作流开源框架ACtiviti的详细配置以及安装和使用
创建ProcessEngine Activiti流程引擎的配置文件是名为activiti.cfg.xml的XML文件.注意与使用Spring方式创建流程引擎是不一样的 使用org.activiti.e ...
- IntelliJ IDEA配置tomcat 教程
1.点击Run-Edit Configurations... 2.点击左侧"+",选择Tomcat Server--Local 3.在Tomcat Server -> Unn ...
- 记一次 .NET 某电商交易平台Web站 CPU爆高分析
一:背景 1. 讲故事 已经连续写了几篇关于内存暴涨的真实案例,有点麻木了,这篇换个口味,分享一个 CPU爆高 的案例,前段时间有位朋友在 wx 上找到我,说他的一个老项目经常收到 CPU > ...
- Mybatis Plus 多租户架构实现(完美教程)
一.背景介绍 多租户技术或称多重租赁技术,简称SaaS,是一种软件架构技术,是实现如何在多用户环境下(此处的多用户一般是面向企业用户)共用相同的系统或程序组件,并且可确保各用户间数据的隔离性. 简单讲 ...
- kubernetes技能图谱
深入剖析Kubernetes-张磊(Kubernetes社区资深成员与项目维护者) Kubernetes集群搭建 ver1.20.5
- SqlServer事务详解(事务隔离性和隔离级别详解)
概述 不少人对于事务的使用局限于begin transaction:开始事务.commit transaction:提交事务.rollback transaction:回滚事务的初步运用. 并且知道使 ...
- IDEA2021.1 安装教程
工欲善其事必先利其器. 一.下载 IDEA 官方下载地址: https://www.jetbrains.com/zh-cn/idea/download/ 二.安装 IDEA 注:安装IDEA之前需要我 ...
- DDD划分领域、子域,核心域,支撑域的目的
名词解释 在DDD兴起的原因以及与微服务的关系中曾举了一个研究桃树的例子,如果要研究桃树,将桃树根据器官分成根.茎.叶.花.果实.种子,这每一种器官都可以认为是一个研究领域,而领域又有更加具体的细分, ...
- GO学习-(26) Go语言操作mongoDB
Go语言操作mongoDB mongoDB是目前比较流行的一个基于分布式文件存储的数据库,它是一个介于关系数据库和非关系数据库(NoSQL)之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的. ...
- 查询rman备份信息常用指令
查询rman备份信息常用指令 ----登陆到rman $rman target / ----以精简的格式查看备份信息 RMAN> list backup of database summar ...