当前位置:网站首页>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 .
边栏推荐
猜你喜欢
Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
分布式事务解决方案
得物客服热线的演进之路
高端了8年,雅迪如今怎么样?
Cinnamon Applet 入门
2022-7-6 初学redis(一)在 Linux 下下载安装并运行 redis
cmake 学习使用笔记(一)
10 pictures open the door of CPU cache consistency
xshell连接服务器把密钥登陆改为密码登陆
供应链供需预估-[时间序列]
随机推荐
[etc.] what are the security objectives and implementation methods that cloud computing security expansion requires to focus on?
Some principles of mongodb optimization
LeetCode_二分搜索_中等_153.寻找旋转排序数组中的最小值
[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
Distributed transaction solution
Final review notes of single chip microcomputer principle
Toraw and markraw
【面试高频题】难度 2.5/5,简单结合 DFS 的 Trie 模板级运用题
如何让join跑得更快?
Flink | multi stream conversion
为租客提供帮助
Build a secure and trusted computing platform based on Kunpeng's native security
Write it down once Net a new energy system thread surge analysis
Oracle advanced (V) schema solution
Realbasicvsr test pictures and videos
Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules
【黑马早报】华为辟谣“军师”陈春花;恒驰5预售价17.9万元;周杰伦新专辑MV 3小时播放量破亿;法华寺回应万元月薪招人...
JS slow motion animation principle teaching (super detail)
[Presto profile series] timeline use
flask session伪造之hctf admin