当前位置:网站首页>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;
}
};
边栏推荐
- Introduce uview into uni app
- Style modification of Mui bottom navigation
- Spark概述
- Redis Key-Value数据库 【高级】
- Google Go to sea entrepreneurship accelerator registration countdown 3 days, entrepreneurs pass through the guide in advance collection!
- CNN visualization technology -- detailed explanation of cam & grad cam and concise implementation of pytorch
- Scheme and implementation of automatic renewal of token expiration
- Go learning notes integration
- Generics and generic constraints of typescript
- Flutter hybrid development: develop a simple quick start framework | developers say · dtalk
猜你喜欢
Community theory | kotlin flow's principle and design philosophy
Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability
步骤详解 | 助您轻松提交 Google Play 数据安全表单
Software testing Q & A
借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力
Shenji Bailian 3.54-dichotomy of dyeing judgment
keepalived安装使用与快速入门
Shenji Bailian 3.53-kruskal
Shenji Bailian 3.52-prim
Unity Shader 学习笔记(3)URP渲染管线带阴影PBR-Shader模板(ASE优化版本)
随机推荐
Step by step | help you easily submit Google play data security form
穀歌出海創業加速器報名倒計時 3 天,創業人闖關指南提前收藏!
Generics and generic constraints of typescript
Spark概述
Let every developer use machine learning technology
[whether PHP has soap extensions installed] a common problem for PHP to implement soap proxy: how to handle class' SoapClient 'not found in PHP
Eco express micro engine system has supported one click deployment to cloud hosting
Test case
在uni-app中引入uView
Go 学习笔记整合
Redis key value database [seckill]
AttributeError: ‘str‘ object has no attribute ‘decode‘
Error creating bean with name 'instanceoperatorclientimpl' defined in URL when Nacos starts
500. Keyboard line
Comment utiliser mitmproxy
Mock simulate the background return data with mockjs
网络相关知识(硬件工程师)
MySQL transaction and isolation level
Current situation analysis of Devops and noops
Frequently asked questions about jetpack compose and material you