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 ...
随机推荐
- 最适合新手的Redis Cluster搭建过程
好记性不如烂笔头,记录分片高可用Redis Cluster的搭建过程 Redis集群演进过程 Redis单节点 主从复制: 复制主要实现了数据的多机备份,以及对于读操作的负载均衡和简单的故障恢复. 故 ...
- [论文阅读笔记] Unsupervised Attributed Network Embedding via Cross Fusion
[论文阅读笔记] Unsupervised Attributed Network Embedding via Cross Fusion 本文结构 解决问题 主要贡献 算法原理 实验结果 参考文献 (1 ...
- Python+Selenium - windows安全中心的弹窗(账号登录)
当出现如下图所示的 Windows安全中心弹窗,需要输入用户名和密码时 如何用Python+selenium跳过这个登录. 步骤: 1.在注册表中三个位置各添加两个东西:iexplore.exe 和 ...
- 2.5D Visual Sound:CVPR2019论文解析
2.5D Visual Sound:CVPR2019论文解析 论文链接: http://openaccess.thecvf.com/content_CVPR_2019/papers/Gao_2.5D_ ...
- Java设计模式:23种设计模式全面解析(超级详细)以及在源码中的应用
从网络上找的设计模式, 很全面,只要把UML类图看懂了, 照着类图将代码实现是很容易的事情. 步骤: 先看懂类图, 然后将代码实现, 之后再看文字 http://c.biancheng.net/des ...
- Java接口以及匿名内部类,静态代码块
接口 接口中只能定义public并且是final的公共静态常量,不允许定义变量. 抽象类可以定义抽象方法和非抽象方法,接口中只能定义公共的,抽象的实例方法. 接口只能由其他接口实现继承 子接口继承的目 ...
- 一篇文章带你搞懂 etcd 3.5 的核心特性
作者 唐聪,腾讯云资深工程师,极客时间专栏<etcd实战课>作者,etcd活跃贡献者,主要负责腾讯云大规模k8s/etcd平台.有状态服务容器化.在离线混部等产品研发设计工作. etcd ...
- BIM,PIM接入GIS 需要解决的关键技术问题
随着技术发展,跨界融合已经不是新鲜事物,近两年BIM.PIM+GIS一张图的提出,给行业注入了一股清流. 为GIS行业发展带来了新的契机,同时也带来了一些新的挑战.面对挑战,本文将剖析BIM.PIM+ ...
- 入门实践丨如何在K3s上部署Web应用程序
在本文中,我们将使用Flask和JavaScript编写的.带有MongoDB数据库的TODO应用程序,并学习如何将其部署到Kubernetes上.这篇文章是针对初学者的,如果你之前没有深度接触过Ku ...
- Win32Api -- 使应用Always on top的几种方法
本文介绍几种使应用一直置于顶层的方法. 问题描述 一般情况下,想要将应用置于顶层,设置其TopMost属性为true即可.对于多个设置了TopMost属性的应用,后激活的在上面. 但有的应用,比如全局 ...