LeetCode(27)Remove Element
题目
Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
分析
这是一道很简单的题目,对数组进行一次遍历,删除与给定值相等的关键字,返回新长度即可。
AC代码
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int i = 0 , k = 0;
while (i < nums.size())
{
if (nums[i] != val)
nums[k++] = nums[i];
i++;
}
return k;
}
};
LeetCode(27)Remove Element的更多相关文章
- 【LeetCode算法-27】Remove Element
LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...
- LeetCode(28)-Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- LeetCode(82)Remove Duplicates from Sorted List
题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...
- LeetCode(26) Remove Duplicates from Sorted Array
题目 Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- LeetCode(203) Remove LinkedList Elements
题目 Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 ...
- LeetCode(83)Remove Duplicates from Sorted List
题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...
- LeetCode(80)Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode(169)Majority Element
题目 Given an array of size n, find the majority element. The majority element is the element that app ...
- LeetCode(19) Remove Nth Node From End of List
题目 Given a linked list, remove the nth node from the end of list and return its head. For example, G ...
随机推荐
- threading多线程模块
1 基本实现 Thread(target=函数名,args=(以元组形式传递的实参,要加",")) th = threading.Thread(target=run,args=(i ...
- Python %s和%r的区别
%s 用str()方法处理对象 %r 用rper()方法处理对象,打印时能够重现它所代表的对象(rper() unambiguously recreate the object it represen ...
- IIR型高斯滤波的原理及实现
二.实现 GIMP中有IIR型高斯滤波的实现,代码位于contrast-retinex.c中,读者可自行查看.下面给出本人实现的核心代码: #include"stdafx.h" t ...
- sublime的几款实用插件
1.CSScomb 用于调整css属性的书写顺序 2.Emmet 缩写神器 3.HTML/CSS/JS Prettify 代码格式化 4.Trimmer 去空格去空行 5.Alignment 代码对齐 ...
- vue-resource emulateJSON的作用
如果Web服务器无法处理编码为application/json的请求,你可以启用emulateJSON选项. 启用该选项后,请求会以application/x-www-form-urlencoded作 ...
- AJPFX总结OpenJDK 和 HashMap大量数据处理时,避免垃圾回收延迟的技巧二
HashMap简史 “Hash Code”这个概念第一次出现是在1953年1月的<Computing literature>中,H. P. Luhn (1896-1964) 在一篇 IB ...
- ES6学习笔记(10)----Set和Map数据结构
参考书<ECMAScript 6入门>http://es6.ruanyifeng.com/ Set和Map数据结构 1.Set 基本用法 Set是一种新的数据结构,它的成员都是唯一 ...
- 26款优秀的Android逆向工程工具
26款优秀的Android逆向工程工具
- CocoaPods安装遇到的坑。
//官方推荐地址 CocoaPods :http://code4app.com/article/cocoapods-install-usage cooped的安装 $(inherited) 报pod ...
- Understanding Scroll Views 深入理解 scroll view 读书笔记
Understanding Scroll Views 深入理解 scroll view 读书笔记 It may be hard to believe, but a UIScrollView is ...