Remove Element(删除数组某一元素)
来源:互联网 发布:法国劳动法改革 知乎 编辑:程序博客网 时间:2024/05/17 06:48
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.class Solution {public: int removeElement(vector<int>& nums, int val) { vector<int>a; for(int i=0;i<nums.size();i++) { if(nums[i]!=val) a.push_back(nums[i]); } nums=a; return a.size(); }};别人的答案:int rm(vector<int> &arr, int value) { int cnt=0; for(int i=0; i<arr.size(); i++) { if(arr[i]==value) cnt++; else if (cnt>0) { arr[i-cnt] = arr[i]; } } return size-cnt; } class Solution {public: int removeElement(int A[], int n, int elem) { // Note: The Solution object is instantiated only once and is reused by each test case. if (n == 0 ) return 0; int len = n; for (int i = 0, pos = 0; i < n; ++i) { if (A[i] == elem) --len; else A[pos++] = A[i]; } return len; }};class Solution {public: int removeElement(int A[], int n, int elem) { // Note: The Solution object is instantiated only once and is reused by each test case. if (n == 0 ) return 0; int len = n; for (int i = 0, pos = 0; i < n; ++i) { if (A[i] == elem) --len; else A[pos++] = A[i]; } return len; }};
0 0
- Remove Element(删除数组某一元素)
- [LeetCode-27] Remove Element(数组元素删除)
- Remove Element 数组删除指定的元素
- Remove Element:删除数组中指定元素
- LeetCode Remove Element 快速删除数组中的某一值
- Remove Element 删除元素
- LeetCode | Remove Element(删除指定元素)
- LeetCode 27. Remove Element(删除元素)
- Remove Element 数组中删除给定元素 @ LeetCode
- LeetCode:Remove Element(删除数组中的特定元素)
- leetcode:Remove Element删除元素
- [C++]LeetCode 27: Remove Element(删除数组中指定元素)
- 【LeetCode-面试算法经典-Java实现】【027-Remove Element(删除数组中指定的元素)】
- [LeetCode-27] Remove Element(从数组删除指定的元素)
- leetcode:Remove Element (删除给定元素)【面试算法题】
- 172.Remove Element-删除元素(容易题)
- 删除序列中的指定元素 Remove Element
- 27. Remove Element 删除指定元素
- Linux驱动之触摸屏
- phpstorm 9 序列号,且用且珍惜!
- Android 侧滑
- 赛码 A+B problem
- apache https配置
- Remove Element(删除数组某一元素)
- 关于计算struct结构体占用空间
- ios-数据库-事务
- 1.7 什么是软件生命周期模型?试比较瀑布模型,快速原型模型,增量模型和螺旋模型的优缺点,说明每种模型的适用范围
- 数据结构中的概念问题
- 时间格式化
- 编译器自动创建的析构函数
- [网络原理]TCP连接是由客户端主动建立的吗?
- Java 根据 HashMap 的 value 进行排序