27.Remove Element
题目:
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
Example 1:
Given nums = [3,2,2,3], val = 3,
Your function should return length = 2, with the first two elements of nums being 2.
It doesn’t matter what you leave beyond the returned length.
Example 2:
Given nums = [0,1,2,2,3,0,4,2], val = 2,
Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.
Note that the order of those five elements can be arbitrary.
It doesn’t matter what values are set beyond the returned length.
解析:给定一个数组,删除数组中指定元素。
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
if(nums.size()==0) return 0;
int j=0;
for(int i=0;i<nums.size();i++)
{
if(nums[i]==val) continue;
else
nums[j++]=nums[i];
}
return j;
}
};
上一篇: 聊天小项目之多客户端聊天
下一篇: 剑指leetcode—旋转图像
推荐阅读
-
Element animation-list must be declared异常解决_html/css_WEB-ITnose
-
element树形列表同时选中子父节点以及回显的实现
-
JavaScript全总结之DOM的Element
-
Vue3.0 + Element Plus|踩坑:表单重置按钮resetForm失效
-
Vue实现Element UI的下拉框默认选中,值来自父组件或异步获取
-
Vue+Element+Select下拉框回显后无法再次选择修改
-
vue element-ui input=textarea无法输入的问题
-
基于vue3.0和element-plus的IP地址输入框
-
Vue + Element el-upload 动态绑定 action 上传地址,上传地址更新不及时
-
vue element-ui resetFields方法重置表单失效