当前位置:网站首页>27. Remove elements
27. Remove elements
2022-06-11 05:17:00 【qq_ twenty-six million three hundred and ninety-one thousand tw】
/* Give you an array nums And a value val, You need In situ Remove all values equal to val The elements of , And return the new length of the array after removal . Don't use extra array space , You have to use only O(1) Additional space and In situ Modify input array . The order of elements can be changed . You don't need to think about the elements in the array that follow the new length . Because it needs to be operated in situ , No auxiliary space , Consider double pointers l,r, distinguish val He Fei val Loop traversal , When the left pointer is val, Right pointer is not val when , In exchange for l,r Value , When l==r, Exit loop , If at present l The value of is val, return l The first number is l, Otherwise return to l The previous number plus the current number l Corresponding elements , namely 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;
};
边栏推荐
- Vins fusion GPS fusion part
- BP neural network derivation + Example
- The data center is evolving towards the "four high" trend, and the OCP network card and the whole cabinet will be delivered into the mainstream of the future data center
- Feynman learning method
- mysql字符串转数组,合并结果集,转成数组
- Deep extension technology: intelligent OCR recognition technology based on deep learning has great potential
- 6 questions to ask when selecting a digital asset custodian
- Concurrent search set
- Paper reproduction: expressive body capture
- C language test question 3 (grammar multiple choice question - including detailed explanation of knowledge points)
猜你喜欢

New library goes online | cnopendata immovable cultural relic data

Technology | image motion drive interpretation of first order motion model

Zed2 camera manual

Tianchi - student test score forecast

MySQL regularly deletes expired data.

Cross modal retrieval | visual representation learning

2021 iccv paper sharing - occlusion boundary detection

Paper reproduction: expressive body capture

Conversion relationship between coordinate systems (ECEF, LLA, ENU)
![[NIPS2021]MLP-Mixer: An all-MLP Architecture for Vision](/img/89/66c30ea8d7969fef76785da1627ce5.jpg)
[NIPS2021]MLP-Mixer: An all-MLP Architecture for Vision
随机推荐
Huawei equipment is configured with cross domain virtual private network
Solving graph problems with union search set
Opencv learning path (2-4) -- Deep parsing cvtcolor function
C language test question 3 (grammar multiple choice question - including detailed explanation of knowledge points)
博途仿真时出现“没有针对此地址组态任何硬件,无法进行修改”解决办法
The solution "no hardware is configured for this address and cannot be modified" appears during botu simulation
lower_ Personal understanding of bound function
Titanic rescued - re exploration of data mining (ideas + source code + results)
Traversal of binary tree -- restoring binary tree by two different Traversals
[opencv learning problems] 1 Namedwindow() and imshow() show two windows in the picture
wxParse解析iframe播放视频
华为设备配置BGP/MPLS IP 虚拟专用网
华为设备配置通过GRE隧道接入虚拟专用网
Using keras to build the basic model yingtailing flower
Simple knowledge distillation
Huawei equipment is configured to access the virtual private network through GRE
Preliminary test of running vins-fusion with zed2 binocular camera
Concurrent search set
KD-Tree and LSH
About custom comparison methods of classes and custom methods of sort functions