题目解析
Given an array nums and a value val, 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 by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
Example 1:
Given nums = [3,2,2,3], val = 3,
Your function should return length = 2, with the first two elements of nums being 2.
It doesn’t matter what you leave beyond the returned length.
整型数组中移除和目标val相同的元素,返回剩下数据的长度n,数组前n元素是结果元素
分析步骤
1、返回非val元素个数n,并且数组前n是非val 元素, 注意顺序无要求
2、除去n个元素外剩余的元素内容也没要求
3、不开辟额外的数组空间
代码演示:
1 | /** |
总结
- 顺序思路实现,优化了下小细节, 主要还是防脑袋秀逗。 思考思考,写写
版权声明:本文为博主原创文章,未经允许不得转载。