当前位置:网站首页>27、移除元素
27、移除元素
2022-06-11 05:08:00 【qq_26391203】
/*给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。 不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。 元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。 因为需要原地操作,没有辅助空间,考虑双指针l,r,区分val和非val 循环遍历,当左指针为val,右指针不为val时,交换l,r的值, 当l==r,退出循环,如果当前l的值为val,返回l前面的个数即l,否则返回l前面的个数加当前l对应的元素,即l+1 */
/** * @param {number[]} nums * @param {number} val * @return {number} */
var removeElement = function(nums, val) {
if(!nums||!nums.length){
return 0;
}
var l=0,r=nums.length-1;
while (l<r){
while (l<r&&nums[l]!==val){
l++;
}
while (l<r&&nums[r]===val){
r--;
}
var tmp=nums[l];
nums[l]=nums[r];
nums[r]=tmp;
}
return nums[l]===val?l:l+1;
};
边栏推荐
- C language test question 3 (advanced program multiple choice questions _ including detailed explanation of knowledge points)
- Google drive download failed, network error
- Leetcode 161 Editing distance of 1 (2022.06.10)
- Huawei device configuration bgp/mpls IP virtual private network command
- JVM tuning V: JVM tuning tools and tuning practice
- Lr-link Lianrui fully understands the server network card
- Introduction to coordinate system in navigation system
- Section V: Recycling Application of asphalt pavement materials
- QT Road (1) -- Introduction to pro file
- Huawei equipment is configured with cross domain virtual private network
猜你喜欢

高斯白噪声(white Gaussian noise,WGN)

English digital converter

华为设备配置BGP/MPLS IP 虚拟专用网地址空间重叠

Paper recommendation: relicv2, can the new self supervised learning surpass supervised learning on RESNET?

Preliminary understanding of DFS and BFS

How to apply for free idea with official documents

点击图标不灵敏咋整?

Target detection - personal understanding of RCNN series

Huawei equipment is configured with cross domain virtual private network
![[aaai 2021 timing action nomination generation] detailed interpretation of bsn++ long article](/img/28/d69a7583036a2076facffcf9098d7e.jpg)
[aaai 2021 timing action nomination generation] detailed interpretation of bsn++ long article
随机推荐
自定义View基础之Layout
Carrier coordinate system inertial coordinate system world coordinate system
Oh my Zsh correct installation posture
Iris dataset - Introduction to machine learning
Thesis 𞓜 jointly pre training transformers on unpaired images and text
Topological sorting
Click the icon is not sensitive how to adjust?
jvm调优五:jvm调优工具和调优实战
C语言试题三(程序选择题进阶_含知识点详解)
Exhibit express: Lianrui will bring three new products of the industry to debut in visionchina (Shenzhen) 2021
华为设备配置跨域虚拟专用网
Apply the intelligent OCR identification technology of Shenzhen Yanchang technology to break through the bottleneck of medical bill identification at one stroke. Efficient claim settlement is not a dr
Paper recommendation: relicv2, can the new self supervised learning surpass supervised learning on RESNET?
Huawei equipment is configured with bgp/mpls IP virtual private network address space overlap
Zed2 camera manual
【Markdown语法高级】 让你的博客更精彩(三:常用图标模板)
Sealem Finance打造Web3去中心化金融平台基础设施
Use acme SH automatically apply for a free SSL certificate
Some details about memory
Simple knowledge distillation