【LeetCode27】【Remove Element】
来源:互联网 发布:unity3d内景 编辑:程序博客网 时间:2024/06/10 06:56
1.题目原文:
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.
2.题意解读:
做过第26道题目之后,这道题理解起来是比较容易的。仍旧是给定一个数组,把数组中所有与目标值(val)不相同的值移到前面。详细就不说了,没做过第26道题的话,可以看我的另一篇博文,链接:http://blog.csdn.net/csdnofheming/article/details/71302260
3.解法:
解法与第26道题的解法相似,仍旧是双指针方法解题,而且运行速度也很快,最快达到击败百分之90多的提交量。
代码如下:
public int removeElement(int[] nums, int val) { int count = 0; for(int num:nums){ if(num!=val){ nums[count++] = num; } } return count;//因为每次"num!=val"成立时,给数组count位置赋值之后,总会给count+1,所以这里不用将count+1. }
4.涉及知识点:
设计知识点与第26道题相似,就不赘述了。
需要注意的一点:该方法返回count值,而不是count+1值,第一次提交时将”renturn count”误写成了”return count++”,幸运的是这个式子是先返回count,再将count+1,所以仍然是Accepted。
- [LeetCode27]Remove Element
- LeetCode27:Remove Element
- leetcode27:Remove Element
- leetcode27: Remove Element
- Leetcode27. Remove Element
- LeetCode27. Remove Element
- leetcode27:Remove Element
- leetcode27~Remove Element
- 【LeetCode27】【Remove Element】
- LeetCode27. Remove Element
- leetcode27. Remove Element
- leetcode27. Remove Element
- LeetCode27 Remove Element
- leetcode27. Remove Element
- LeetCode27. Remove Element
- leetcode27. Remove Element
- LeetCode27. Remove Element
- LeetCode27 Remove Element
- 编写poc和exploit的几款常用工具介绍
- 辨析文件描述符和文件指针
- 2017微信公开课·张小龙演讲全文(下)
- 【Unity】UGUI的Text各种小问题
- s3c2440 dm9000网卡驱动移植
- 【LeetCode27】【Remove Element】
- python3.4+KNN
- jQuery文档初始化函数原理
- CentOS 6 与 CentOS 7 bonding实现
- Fork/Join框架及其性能介绍
- 每日一题 No.41 最长上升序列
- socket之bind
- 最佳的MongoDB客户端管理工具---MongoVUE
- zigbee 指定节点入网 函数ZDO_JoinIndicationCB()