当前位置:网站首页>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;
}
};
边栏推荐
- 神机百炼3.54-染色法判定二分图
- 外部中断无法进入,删代码再还原就好......记录这个想不到的bug
- Little bear sect manual query and ADC in-depth study
- Software testing Q & A
- MySQL transaction and isolation level
- Community theory | kotlin flow's principle and design philosophy
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ 1: Maximum palindromes
- DRM display framework as I understand it
- Several keywords in C language
- Some descriptions of Mipi protocol of LCD
猜你喜欢

Step by step | help you easily submit Google play data security form

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

keepalived安装使用与快速入门

Reading notes of cgnf: conditional graph neural fields

借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力

Unity Shader 学习笔记(3)URP渲染管线带阴影PBR-Shader模板(ASE优化版本)

51单片机——ADC讲解(A/D转换、D/A转换)

MySQL transaction and isolation level

Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability

Zabbix Server trapper 命令注入漏洞 (CVE-2017-2824)
随机推荐
Nacos 启动报错 Error creating bean with name ‘instanceOperatorClientImpl‘ defined in URL
51 single chip microcomputer - ADC explanation (a/d conversion, d/a conversion)
Current situation analysis of Devops and noops
Eco express micro engine system has supported one click deployment to cloud hosting
Redis Key-Value数据库【初级】
JWT tool class
PHP extensions
Deep learning classification network -- Network in network
uni-app开发中遇到的问题(持续更新)
社区说|Kotlin Flow 的原理与设计哲学
Mathematical statistics and machine learning
Memcached installation
Little bear sect manual query and ADC in-depth study
PHP development and testing WebService (soap) -win
The real definition of open source software
Lambda expressions and method references
TI毫米波雷达学习(一)
来自读者们的 I/O 观后感|有奖征集获奖名单
Contest3147 - game 38 of 2021 Freshmen's personal training match_ 1: Maximum palindromes
【C语言】简单实现扫雷游戏