leetcode 27 removeElement
来源:互联网 发布:优化产业结构什么意思 编辑:程序博客网 时间:2024/05/22 09:47
Given an array and a value, 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 in place with constant memory.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
Example:
Given input array nums = [3,2,2,3], val = 3
Your function should return length = 2, with the first two elements of nums being 2.
class Solution {public: int removeElement(vector<int>& nums, int val) { typedef vector<int>::iterator it; it i = nums.begin(); it j = nums.end(); while (i != j) { while (i != j && *i != val) ++i; if (i == j) break; --j; while (i != j && *j == val) --j; if (i != j) *i = *j; } nums.erase(j, nums.end()); return nums.size(); }}; // runtime contribution 64.8%
hehe
class Solution {public: int removeElement(vector<int>& nums, int val) { nums.erase(remove(nums.begin(), nums.end(), val), nums.end()); return nums.size(); }}; // runtime contribution 10.05%
参考后
class Solution {public: int removeElement(vector<int>& nums, int val) { typedef vector<int>::size_type sz; sz size = nums.size(); for (sz i = 0; i < size;) { if (nums[i] == val) { nums[i] = nums[size - 1]; --size; }else ++i; } nums.erase(nums.begin() + size, nums.end()); return size; }}; // runtime contribution 64.8%
阅读全文
0 0
- leetcode 27 removeElement
- LeetCode - RemoveElement
- LeetCode 之RemoveElement
- LeetCode之RemoveElement
- removeElement
- RemoveElement
- 27.RemoveElement(删除一个元素)--Array
- 利用ArrayUtils.removeElement()去掉List中(ˇˍˇ) 想~去的元素。。未完善
- 自定义对象new一个匿名对象以后,removeElement 对象的操作瓶颈
- leetcode-27
- LeetCode #27
- leetcode 27
- [Leetcode]Leetcode 27:Remove Element
- leetcode note--leetcode 27 Remove Element
- LeetCode(27)Remove Element
- [leetcode 27] Remove Element
- Leetcode【27】:Implement strStr
- LeetCode 27 Valid Palindrome
- python里使用select模块
- LeetCode--Maximum Subarray
- 转义
- ConstraintLayout 使用初探
- http和https的区别以及超全局变量
- leetcode 27 removeElement
- Java I/O流总结
- 工件调度
- BZOJ2716: [Violet 3]天使玩偶(CDQ分治)
- OSGI(1)_入门
- Codeforeces 849A Odds and Ends
- rman restore controlfile
- (二)依赖项属性
- 签到