LeetCode 27. Remove Element
来源:互联网 发布:玛祖铭立 知乎 编辑:程序博客网 时间:2024/06/03 18:30
27. Remove Element
Description
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 = 3Your function should return length = 2, with the first two elements of nums being 2.
Solution
- 题意即给定一个数组和一个数字,要求你将其中和这个数字相同的数删除并返回剩下的数组,不论顺序。
- 我们换一个角度来想,就是寻找该数组中和给定数字不同的数字组成的数字,文字不便描述,详见代码:
// C/C++class Solution {public: int removeElement(vector<int>& nums, int val) { int j = 0; for (int i = 0;i < nums.size();i++) { if (val != nums[i]) nums[j++] = nums[i]; } return j; }};
# pythonclass Solution(object): def removeElement(self, nums, val): rnt = 0 for i in range(len(nums)): if nums[i] != val: nums[rnt] = nums[i] rnt += 1 return rnt
阅读全文
0 0
- [LeetCode]27.Remove Element
- LeetCode --- 27. Remove Element
- LeetCode 27.Remove Element
- [Leetcode] 27. Remove Element
- [Leetcode] 27.Remove Element
- 【leetcode】27. Remove Element
- 【leetcode】27. Remove Element
- Leetcode 27. Remove Element
- LeetCode 27. Remove Element
- 27. Remove Element LeetCode
- 【LeetCode】27. Remove Element
- leetcode 27. Remove Element
- leetcode 27. Remove Element
- [LeetCode]27. Remove Element
- LeetCode 27. Remove Element
- [leetcode] 27. Remove Element
- [leetcode] 27. Remove Element
- leetcode 27. Remove Element
- 用windows cmd编译c++程序及其他
- HDU 2612
- 剑指offer——31.整数中1出现的次数
- 悲观锁和乐观锁
- 自定义标签多选更改背景图片
- LeetCode 27. Remove Element
- C# 窗口间的通讯
- IP地址为140.123.0.0的地址是B类地址,若要切割为10个子网,而且都要连接上Internet,请问子网掩码应设为
- Docker+Gogs搭建个人Git服务
- python爬虫练习4:刷博客浏览量
- 黑盒测试、白盒测试、灰盒测试之间区别
- Redis PFADD
- 管道
- bzoj 1060: [ZJOI2007]时态同步 树形dp