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 ...
随机推荐
- ASP.NET Core MVC 入门到精通 - 3. 使用MediatR
ASP.NET Core MVC 入门到精通 - 3. 使用MediatR 环境: .NET 5 ASP.NET Core MVC (project) 1. MediatR MediatR .NET中 ...
- 用户从控制台输入n个整数,比较并输出其中的最大值和最小值
package judgment;import java.util.Scanner;/** * 用户输入n个整数,找出最大值和最小值并打印输出 */public class Judgment { pu ...
- 多表联合查询 - 基于注解SQL
作者:汤圆 个人博客:javalover.cc 前言 背景:Spring Boot + MybatisPlus 用MybatisPlus就是为了不写SQL,用起来方便: 但是如果需要多表联合查询,还是 ...
- 重新整理 .net core 实践篇—————配置系统之间谍[八](文件监控)
前言 前文提及到了当我们的配置文件修改了,那么从 configurationRoot 在此读取会读取到新的数据,本文进行扩展,并从源码方面简单介绍一下,下面内容和前面几节息息相关. 正文 先看一下,如 ...
- MongoDB学习笔记:文档Crud Shell
MongoDB学习笔记:文档Crud Shell 文档插入 一.插入语法 db.collection.insertOne() 将单个文档插入到集合中.db.collection.insertMan ...
- GO语言基础---值传递与引用传递
package main import ( "fmt" ) /* 值传递 函数的[形式参数]是对[实际参数]的值拷贝 所有对地址中内容的修改都与外界的实际参数无关 所有基本数据类型 ...
- Mysql索引创建及删除
1.索引 MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的My ...
- XLearning - 深度学习调度平台
XLearning - 深度学习调度平台 软件简介 XLearning **** 是奇虎 360 开源的一款支持多种机器学习.深度学习框架调度系统.基于 Hadoop Yarn 完成了对TensorF ...
- vulhub-struct2-s2-005
0x00 漏洞原理 s2-005漏洞的起源源于S2-003(受影响版本: 低于Struts 2.0.12),struts2会将http的每个参数名解析为OGNL语句执行(可理解为java代码).O ...
- Java面试必知必会:基础
面试考察的知识点多而杂,要完全掌握需要花费大量的时间和精力.但是面试中经常被问到的知识点却没有多少,你完全可以用 20% 的时间去掌握 80% 常问的知识点. 一.基础 包括: 杂七杂八 面向对象 数 ...