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++实现)的更多相关文章

  1. [Leetcode][Python]27: Remove Element

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...

  2. 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  ...

  3. 【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 ...

  4. 【LeetCode】27 - Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  5. 【一天一道LeetCode】#27. Remove Element

    一天一道LeetCode系列 (一)题目 Given an array and a value, remove all instances of that value in place and ret ...

  6. 【LeetCode】27. Remove Element 解题报告(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 记录起始位置 日期 题目地址:https:/ ...

  7. 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 ...

  8. LeetCode:27. Remove Element(Easy)

    1. 原题链接 https://leetcode.com/problems/remove-element/description/ 2. 题目要求 给定一个整数数组 nums[ ] 和一个整数 val ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. go输出九九乘法表和金字塔

    用go语言实现输出九九乘法表和金字塔: 1.输出九九乘法表: 代码示例: package main import ( "fmt" "time" ) //实现99 ...

  2. spring模板

    applicationContext <?xml version="1.0" encoding="UTF-8"?> <beans xmlns= ...

  3. 结构感知图像修复:ICCV2019论文解析

    结构感知图像修复:ICCV2019论文解析 StructureFlow: Image Inpainting via Structure-aware Appearance Flow 论文链接: http ...

  4. 地理围栏API服务开发

    地理围栏API服务开发 要使用华为地理围栏服务API,需要确保设备已经下载并安装了HMS Core(APK),并将Location Kit的SDK集成到项目中. 指定应用权限 如果需要使用地理围栏服务 ...

  5. CUDA C++编程接口:编译

    CUDA C++编程接口:编译 一.概述 CUDA C++为熟悉C++编程语言的用户提供了一个简单的路径,以方便地编写程序以执行该设备. 它由一组最小的扩展到C++语言和运行库. 在编程模型中引入了核 ...

  6. nvGRAPH原理概述

    nvGRAPH原理概述 nvGRAPH的API参考分析. 简介 数据分析是高性能计算的不断增长的应用.许多高级数据分析问题可以称为图形问题.反过来,当今许多常见的图形问题也可以称为稀疏线性代数.这是N ...

  7. 英特尔® 至强® 平台集成 AI 加速构建数据中心智慧网络

    英特尔 至强 平台集成 AI 加速构建数据中心智慧网络 SNA 通过 AI 方法来实时感知网络状态,基于网络数据分析来实现自动化部署和风险预测,从而让企业网络能更智能.更高效地为最终用户业务提供支撑. ...

  8. python+selenium基础篇,By定位元素

    1.By定位和find_element_by_XXXXXX是一样的,如下图所示,定位元素的方法都是一样的 2.使用By定位代码如下所示 from selenium import webdriver f ...

  9. MySQL必知必会复习笔记(1)

    MySQL必知必会笔记(一) MySQL必知必会是一本很优秀的MySQL教程书,并且相当精简,在日常中甚至能当成一本工作手册来查看.本系列笔记记录的是:1.自己记得不够牢的代码:2.自己觉得很重要的代 ...

  10. Python OpenCV图片转视频 工具贴(三)

    Python OpenCV图片转视频 粘贴即用,注意使用时最好把自己的文件按照数字顺序命名.按照引导输入操作. # 一键傻瓜式引导图片串成视频 # 注意使用前最好把文件命名为数字顺序格式 import ...