当前位置:网站首页>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 .
边栏推荐
- Centso7 OpenSSL error Verify return code: 20 (unable to get local issuer certificate)
- Esp32 series column
- Final review notes of single chip microcomputer principle
- Realize the IP address home display function and number home query
- PHP - laravel cache
- Scrapy教程经典实战【新概念英语】
- Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
- Getting started with cinnamon applet
- Flink | 多流转换
- MySQL error 28 and solution
猜你喜欢
MySQL error 28 and solution
为租客提供帮助
JS缓动动画原理教学(超细节)
Centso7 OpenSSL error Verify return code: 20 (unable to get local issuer certificate)
Milkdown control icon
最佳实践 | 用腾讯云AI意愿核身为电话合规保驾护航
LED light of single chip microcomputer learning notes
交付效率提升52倍,运营效率提升10倍,看《金融云原生技术实践案例汇编》(附下载)
Indoor ROS robot navigation commissioning record (experience in selecting expansion radius)
2022-7-6 初学redis(一)在 Linux 下下载安装并运行 redis
随机推荐
Problems that cannot be accessed in MySQL LAN
Signal strength (RSSI) knowledge sorting
MongoDB复制(副本集)总结
供应链供需预估-[时间序列]
Final review notes of single chip microcomputer principle
单片机原理期末复习笔记
单片机学习笔记之点亮led 灯
作战图鉴:12大场景详述容器安全建设要求
[learning notes] segment tree selection
实现IP地址归属地显示功能、号码归属地查询
JS function returns multiple values
Cinnamon Applet 入门
Summary of import, export, backup and recovery of mongodb
[dark horse morning post] Huawei refutes rumors about "military master" Chen Chunhua; Hengchi 5 has a pre-sale price of 179000 yuan; Jay Chou's new album MV has played more than 100 million in 3 hours
Detr introduction
Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
【黑马早报】华为辟谣“军师”陈春花;恒驰5预售价17.9万元;周杰伦新专辑MV 3小时播放量破亿;法华寺回应万元月薪招人...
[QNX Hypervisor 2.2用户手册]6.3.4 虚拟寄存器(guest_shm.h)
LED light of single chip microcomputer learning notes
PAcP learning note 3: pcap method description