当前位置:网站首页>2022-7-6 Leetcode27. Remove the element - I haven't done the problem for a long time. It's such an embarrassing day for double pointers
2022-7-6 Leetcode27. Remove the element - I haven't done the problem for a long time. It's such an embarrassing day for double pointers
2022-07-07 13:38:00 【weixin_ fifty-one million one hundred and eighty-seven thousand】
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int leftIdx = 0;
int rightIdx = nums.size()-1;
while (leftIdx <= rightIdx){
while (leftIdx <= rightIdx && nums[leftIdx] != val){
leftIdx++;
}
while (leftIdx <= rightIdx&& nums[rightIdx] == val){
rightIdx--;
}
if (leftIdx < rightIdx)
nums[leftIdx++] = nums[rightIdx--];
}
return leftIdx;
}
};
Two points to pay attention to :
1)while (leftIdx < nums.size() && nums[leftIdx++] != val);
You can't write it like that , Because if the current !=val, It will also move forward one
2) And can't write leftIdx < nums.size().
Because it's possible rightIdx The following numbers are val, But the pointer on the right has moved to the front .
边栏推荐
- 靠卖概念上市,认养一头牛能走多远?
- Cinnamon taskbar speed
- Build a secure and trusted computing platform based on Kunpeng's native security
- Xshell connection server changes key login to password login
- 单片机学习笔记之点亮led 灯
- php——laravel缓存cache
- RealBasicVSR测试图片、视频
- 云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
- Talk about pseudo sharing
- PAcP learning note 3: pcap method description
猜你喜欢
QQ的药,腾讯的票
2022-7-6 使用SIGURG来接受外带数据,不知道为什么打印不出来
分布式事务解决方案
2022-7-6 初学redis(一)在 Linux 下下载安装并运行 redis
Indoor ROS robot navigation commissioning record (experience in selecting expansion radius)
High end for 8 years, how is Yadi now?
Error lnk2019: unresolved external symbol
【堡垒机】云堡垒机和普通堡垒机的区别是什么?
Cinnamon Applet 入门
实现IP地址归属地显示功能、号码归属地查询
随机推荐
Some principles of mongodb optimization
【等保】云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
postgresql array类型,每一项拼接
MongoDB的用户管理总结
2022-7-6 Leetcode27.移除元素——太久没有做题了,为双指针如此狼狈的一天
ESP32系列专栏
工具箱之 IKVM.NET 项目新进展
QQ的药,腾讯的票
User management summary of mongodb
ESP32构解工程添加组件
[QNX Hypervisor 2.2用户手册]6.3.4 虚拟寄存器(guest_shm.h)
Learning breakout 2 - about effective learning methods
Use of polarscatter function in MATLAB
Server to server (S2S) event (adjust)
【日常训练--腾讯精选50】231. 2 的幂
How far can it go to adopt a cow by selling the concept to the market?
clion mingw64中文乱码
LED light of single chip microcomputer learning notes
Digital IC Design SPI
2022-7-6 Leetcode 977.有序数组的平方