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 ...
随机推荐
- JQuery.Gantt开发指南(转)
说明 日前需要用到甘特图,以下转载内容源自网络. • 概述 1.JQuery.Gantt是一个开源的基于JQuery库的用于实现甘特图效果的可扩展功能的JS组件库. •前端页面 o 资源引用 首先我们 ...
- Flink-cdc实时读postgresql
由于公司业务需要,需要实时同步pgsql数据,我们选择使用flink-cdc方式进行 架构图: 前提步骤: 1,更改配置文件postgresql.conf # 更改wal日志方式为logicalwal ...
- uboot通过NFS挂载ubuntu根文件系统
由于工作需要,在做app开发或系统移植时,经常需要编辑系统后重新烧写异常麻烦.通过NFS挂载根文件系统就不需要每次更改系统后再进行编译和烧写,等开发完成后一次烧写即可完成. 一.准备材料 可以根据自己 ...
- 鱼眼摄像头SLAM
鱼眼摄像头SLAM 在机器人技术.摄影测量学和计算机视觉等领域,鲁棒相机位姿估计是许多视觉应用的核心.近年来,在复杂.大规模的室内外环境中,人们越来越关注相机位姿估计方法的实时性.通用性和可操作性.其 ...
- 深度学习加速器堆栈Deep Learning Accelerator Stack
深度学习加速器堆栈Deep Learning Accelerator Stack 通用张量加速器(VTA)是一种开放的.通用的.可定制的深度学习加速器,具有完整的基于TVM的编译器堆栈.设计了VTA来 ...
- 那些年,我们一起做过的KNX智能化控制经典案例!
那些年,我们一起做过的KNX经典案例! 光阴之箭已经穿越年轮,抵达2021 GVS在2008年加入KNX国际协会,成为中国首批引进KNX标准的企业,此后,还率先研发出基于KNX的核心协议栈,定标准,做 ...
- python----日志模块loggin的使用,按日志级别分类写入文件
1.日志的级别 日志一共分为5个等级,从低到高分别是: 级别 说明 DEBUG 输出详细的运行情况,主要用于调试. INFO 确认一切按预期运行,一般用于输出重要运行情况. WARNING 系统运行时 ...
- Kubernetes 实战——发现应用(Service)
一.简介 服务:一种为一组功能相同的 Pod 提供单一不变的接入点的资源.服务 IP 和端口不会改变 对服务的连接会被路由到提供该服务的任意一个 Pod 上(负载均衡) 服务通过标签选择器判断哪些 P ...
- 【VBA】字符串处理
InStr 函数:查找字符串 1 Sub InStr函数() 2 Dim strTemp As String 3 strTemp = "=AAA=BBB=C" 4 Debug.Pr ...
- sync.waitgroup ----等待goroutine的执行完成
可以尝试改变wg.add里的值,改变wg.wait,或者wg.done的出现次数以及位置. 感受它的使用