当前位置:网站首页>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;
}
};
边栏推荐
- Detailed notes of ES6
- php读文件(读取json文件,转换为数组)
- Web页面用户分步操作引导插件driver.js
- 【C语言】简单实现扫雷游戏
- Cookie plugin and localforce offline storage plugin
- Use some common functions of hbuilderx
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ F: Polyhedral dice
- token过期自动续费方案和实现
- 492. Construction rectangle
- 495. Timo attack
猜你喜欢

Deep learning classification network -- vggnet

PHP 开发与测试 Webservice(SOAP)-Win

ES6的详细注解

ZABBIX server trap command injection vulnerability (cve-2017-2824)

社区说|Kotlin Flow 的原理与设计哲学

脑与认知神经科学Matlab Psytoolbox认知科学实验设计——实验设计四

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

Google Play Academy 组队 PK 赛,正式开赛!

神机百炼3.54-染色法判定二分图

神机百炼3.52-Prim
随机推荐
Servlet web XML configuration details (3.0)
Web页面用户分步操作引导插件driver.js
神机百炼3.54-染色法判定二分图
Lambda 表达式 和 方法引用
Happy Lantern Festival | Qiming cloud invites you to guess lantern riddles
Shenji Bailian 3.54-dichotomy of dyeing judgment
数据回放伴侣Rviz+plotjuggler
STC8H8K系列匯編和C51實戰——數碼管顯示ADC、按鍵串口回複按鍵號與ADC數值
Contest3145 - the 37th game of 2021 freshman individual training match_ H: Eat fish
Software testing Q & A
数据库学习总结5
CNN visualization technology -- detailed explanation of cam & grad cam and concise implementation of pytorch
PHP array to XML
Google Go to sea entrepreneurship accelerator registration countdown 3 days, entrepreneurs pass through the guide in advance collection!
PHP read file (read the specified line containing a string in the file)
STC8H8K系列汇编和C51实战——串口发送菜单界面选择不同功能
Page printing plug-in print js
【C语言】简单实现扫雷游戏
Unity Shader 学习笔记(3)URP渲染管线带阴影PBR-Shader模板(ASE优化版本)
页面打印插件print.js