当前位置:网站首页>27. 移除元素
27. 移除元素
2022-06-25 06:41:00 【AlbertOS】
引入
给你一个数组 n u m s nums nums 和一个值 v a l val val,你需要 原地 移除所有数值等于 v a l val val 的元素,并返回移除后数组的新长度。
不要使用额外的数组空间,你必须仅使用 O ( 1 ) O(1) O(1) 额外空间并 原地 修改输入数组。
元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。
示例
输入:nums = [3,2,2,3], val = 3
输出:2, nums = [2,2]
解释:函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。你不需要考虑数组中超出新长度后面的元素。例如,函数返回的新长度为 2 ,而 nums = [2,2,3,3] 或 nums = [2,2,0,0],也会被视作正确答案。
输入:nums = [0,1,2,2,3,0,4,2], val = 2
输出:5, nums = [0,1,4,0,3]
解释:函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。
题解
这个用双指针覆盖你要删除的元素位置就好了,最后在修改数组的长度就好了,这里我使用容器vector存放数组,它可以智能调整数组长度,比自己调整方便~
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int n = nums.size();
int left = 0;
for (int right = 0; right < n; right++) {
if (nums[right] != val) {
nums[left] = nums[right];
left++;
}
}
return left;
}
};
总结
其实今天做了一道困难题,但是还有待提高的地方,就不发那个了~
边栏推荐
- Chuantu microelectronics 𞓜 subminiature package isolated half duplex 485 transceiver
- 【蒸馏】PointDistiller: Structured Knowledge DistillationTowards Efficient and Compact 3D Detection
- 微信小程序入门记录
- 差点被这波Handler 面试连环炮带走~
- Chuantuwei ca-is3720lw alternative material No. iso7820fdw
- MySQL face Scripture eight part essay
- Sichuan earth microelectronics ca-is1300 isolated operational amplifier for current detection is on the market
- Without "rice", you can cook "rice". Strategy for retrieving missing ground points under airborne lidar forest using "point cloud intelligent mapping"
- IAR compiler flashback
- OAuth 2.0 one click login
猜你喜欢

Tupu software digital twin 3D wind farm, offshore wind power of smart wind power

CPDA | how to start the growth path of data analysts?

npm install 报错 : gyp ERR! configure error

Modular programming of wireless transmission module nRF905 controlled by single chip microcomputer

Summary of small problems in smartbugs installation

VectorDraw Web Library 10.10

Research on 3D model retrieval method based on two channel attention residual network - Zhou Jie - paper notes

What if there is no point in data visualization?

Three years of continuous decline in revenue, Tiandi No. 1 is trapped in vinegar drinks

OAuth 2.0 one click login
随机推荐
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_ scale_ factor‘
Three years of continuous decline in revenue, Tiandi No. 1 is trapped in vinegar drinks
IAR compiler flashback
Bicubic difference
Elk + filebeat log parsing, log warehousing optimization, logstash filter configuration attribute
“空间转换”显著提升陡崖点云的地面点提取质量
个人域名和企业域名的区别
GUI pull-down menu of unity3d evil door implementation dropdown design has no duplicate items
[batch dos-cmd command - summary and summary] - file and directory operation commands (MD, RD, xcopy, dir, CD, set, move, copy, del, type, sort)
Introduction to Sichuan Tuwei ca-is3082wx isolated rs-485/rs-422 transceiver
单位转换-毫米转像素-像素转毫米
Function template_ Class template
Distributed quorum NWR of the alchemy furnace of the Supreme Master
(tool class) quickly add time to code in source insight
Full range of isolator chips with integrated isolated power supply
Tupu software digital twin 3D wind farm, offshore wind power of smart wind power
[Batch dos - cmd Command - Summary and Summary] - cmd extension Command, extension Function (CMD / E: on, CMD / E: off)
(tool class) use SecureCRT as the communication medium
Path planner based on time potential function in dynamic environment
Domestic MCU perfectly replaces STM chip model of Italy France