当前位置:网站首页>LeetCode 27. 移除元素
LeetCode 27. 移除元素
2022-07-02 06:07:00 【大白羊_Aries】
题目描述
解法
解法还是快慢指针:如果 fast 遇到值为 val 的元素,则直接跳过,否则就赋值给 slow 指针,并让 slow 前进一步
但是,注意这里和有序数组去重(26. 删除有序数组中的重复项)的解法有一个细节差异,我们这里是先给 nums[slow] 赋值然后再给 slow++,这样可以保证 nums[0…slow-1] 是不包含值为 val 的元素的,最后的结果数组长度就是 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;
}
};
边栏推荐
- 脑与认知神经科学Matlab Psytoolbox认知科学实验设计——实验设计四
- Redis key value database [primary]
- Shenji Bailian 3.52-prim
- JWT工具类
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ E: Listen to songs and know music
- Spark概述
- Ros2 --- lifecycle node summary
- 借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力
- 51单片机——ADC讲解(A/D转换、D/A转换)
- Web page user step-by-step operation guide plug-in driver js
猜你喜欢

CNN visualization technology -- detailed explanation of cam & grad cam and concise implementation of pytorch

数据回放伴侣Rviz+plotjuggler

经典文献阅读之--SuMa++

Spark概述

Can't the dist packaged by vite be opened directly in the browser

500. 键盘行

Brain and cognitive neuroscience matlab psychoolbox cognitive science experimental design - experimental design 4

Memcached installation

Contest3147 - game 38 of 2021 Freshmen's personal training match_ E: Listen to songs and know music

Contest3147 - game 38 of 2021 Freshmen's personal training match_ A: chicken
随机推荐
Redis Key-Value数据库 【秒杀】
ROS2----LifecycleNode生命周期节点总结
Google Play Academy 组队 PK 赛,正式开赛!
Happy Lantern Festival | Qiming cloud invites you to guess lantern riddles
Spark概述
Nacos 启动报错 Error creating bean with name ‘instanceOperatorClientImpl‘ defined in URL
Cookie plugin and localforce offline storage plugin
Software testing - concept
ESP8266与STC8H8K单片机联动——天气时钟
Can't the dist packaged by vite be opened directly in the browser
On Web server
token过期自动续费方案和实现
How vite is compatible with lower version browsers
官方零基础入门 Jetpack Compose 的中文课程来啦!
Let every developer use machine learning technology
穀歌出海創業加速器報名倒計時 3 天,創業人闖關指南提前收藏!
Talking about MySQL database
Contest3147 - game 38 of 2021 Freshmen's personal training match_ F: Polyhedral dice
外部中断无法进入,删代码再还原就好......记录这个想不到的bug
在uni-app中引入uView