当前位置:网站首页>LeetCode 27. Removing Elements
LeetCode 27. Removing Elements
2022-07-02 06:10:00 【Great white sheep_ Aries】
Title Description
solution
Solution or speed pointer : If fast Encountered value is val The elements of , Then skip directly , Otherwise, it will be assigned to slow The pointer , And let slow Take a step forward
however , Notice here and the ordered array de duplication (26. Remove duplicate items from an ordered array ) There is one detail difference in the solution of , We are here to give nums[slow] Assign a value and then give it to slow++, This ensures nums[0…slow-1] Does not contain a value of val The elements of the , The final result is the length of the array slow
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
if (nums.size() == 0) return 0;
int slow = 0, fast = 0;
while (fast < nums.size())
{
if (nums[fast] != val)
{
nums[slow] = nums[fast];
slow++;
}
fast++;
}
return slow;
}
};
边栏推荐
- The real definition of open source software
- Common websites for Postgraduates in data mining
- Generic classes and parameterized classes of SystemVerilog
- 官方零基础入门 Jetpack Compose 的中文课程来啦!
- Data playback partner rviz+plotjuggler
- Google Play Academy 组队 PK 赛,正式开赛!
- [C language] screening method for prime numbers
- Deep learning classification network -- Network in network
- On Web server
- LeetCode 47. 全排列 II
猜你喜欢

Summary of MySQL constraints

ROS2----LifecycleNode生命周期节点总结

数据回放伴侣Rviz+plotjuggler

Zabbix Server trapper 命令注入漏洞 (CVE-2017-2824)

Sumo tutorial Hello World

Ti millimeter wave radar learning (I)

亚马逊aws数据湖工作之坑1

Deep learning classification network -- vggnet

复杂 json数据 js前台解析 详细步骤《案例:一》

脑与认知神经科学Matlab Psytoolbox认知科学实验设计——实验设计四
随机推荐
Google Go to sea entrepreneurship accelerator registration countdown 3 days, entrepreneurs pass through the guide in advance collection!
Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table
AttributeError: ‘str‘ object has no attribute ‘decode‘
Database learning summary 5
深度学习分类网络--VGGNet
神机百炼3.52-Prim
LeetCode 90. 子集 II
Spark overview
Go learning notes integration
Flutter hybrid development: develop a simple quick start framework | developers say · dtalk
Lambda 表达式 和 方法引用
LeetCode 78. 子集
借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力
LeetCode 47. 全排列 II
经典文献阅读之--Deformable DETR
Deep learning classification network -- Network in network
Mathematical statistics and machine learning
网络相关知识(硬件工程师)
JWT工具类
STC8H8K系列汇编和C51实战——数码管显示ADC、按键串口回复按键号与ADC数值