当前位置:网站首页>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;
}
};
边栏推荐
- Go 学习笔记整合
- Web页面用户分步操作引导插件driver.js
- Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability
- Shenji Bailian 3.53-kruskal
- BGP 路由優選規則和通告原則
- Deep learning classification network -- vggnet
- 数据回放伴侣Rviz+plotjuggler
- Current situation analysis of Devops and noops
- Shenji Bailian 3.54-dichotomy of dyeing judgment
- Redis key value database [advanced]
猜你喜欢

ES6的详细注解

Community theory | kotlin flow's principle and design philosophy

神机百炼3.52-Prim

Flutter hybrid development: develop a simple quick start framework | developers say · dtalk

Mathematical statistics and machine learning

CNN可视化技术 -- CAM & Grad-CAM详解及pytorch简洁实现

Web components series (VIII) -- custom component style settings

Contest3147 - game 38 of 2021 Freshmen's personal training match_ A: chicken

Shenji Bailian 3.54-dichotomy of dyeing judgment

深度学习分类网络--VGGNet
随机推荐
BGP中的状态机
深度学习分类网络--VGGNet
Ros2 --- lifecycle node summary
cookie插件和localForage离线储存插件
Error creating bean with name 'instanceoperatorclientimpl' defined in URL when Nacos starts
How to use mitmproxy
数据回放伴侣Rviz+plotjuggler
来自读者们的 I/O 观后感|有奖征集获奖名单
[C language] simple implementation of mine sweeping game
MySQL transaction and isolation level
Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed
数据库学习总结5
Let every developer use machine learning technology
Linkage between esp8266 and stc8h8k single chip microcomputer - Weather Clock
Go learning notes integration
神机百炼3.53-Kruskal
I/o impressions from readers | prize collection winners list
Lucene Basics
网络相关知识(硬件工程师)
Format check JS