当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

Ros2 --- lifecycle node summary

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

Regular expression summary

Deep learning classification network -- alexnet

Summary of MySQL constraints

来自读者们的 I/O 观后感|有奖征集获奖名单

Little bear sect manual query and ADC in-depth study

Unity shader learning notes (3) URP rendering pipeline shaded PBR shader template (ASE optimized version)

让每一位开发者皆可使用机器学习技术

500. Keyboard line
随机推荐
神机百炼3.52-Prim
Unity Shader 学习笔记(3)URP渲染管线带阴影PBR-Shader模板(ASE优化版本)
Lambda expressions and method references
Talking about MySQL database
Shenji Bailian 3.52-prim
Happy Lantern Festival | Qiming cloud invites you to guess lantern riddles
Memcached installation
Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table
STC8H8K系列汇编和C51实战——数码管显示ADC、按键串口回复按键号与ADC数值
Stc8h8k series assembly and C51 actual combat - serial port sending menu interface to select different functions
Spark overview
数据回放伴侣Rviz+plotjuggler
Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
Scheme and implementation of automatic renewal of token expiration
The real definition of open source software
Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed
网络相关知识(硬件工程师)
Deep learning classification network -- vggnet
Unity shader learning notes (3) URP rendering pipeline shaded PBR shader template (ASE optimized version)
Problems encountered in uni app development (continuous update)