我是靠谱客的博主 彩色大山,这篇文章主要介绍leetCode 27.Remove Element (删除元素) 解题思路和方法,现在分享给大家,希望可以做个参考。

Remove Element 


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.


思路:此题和26题一脉相承,算法上不难,具体如代码所示:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Solution { public int removeElement(int[] nums, int val) { int len = nums.length; int tempLen = len; int step = 0;//每个元素需要向前转移的距离 for(int i = 0; i < len; i++){ if(nums[i] == val){ step++;//若相等步长+1 tempLen--;//每一个相等的元素长度减少1 }else{ nums[i-step] = nums[i];//元素前移n个步长 } } return tempLen; } }


最后

以上就是彩色大山最近收集整理的关于leetCode 27.Remove Element (删除元素) 解题思路和方法的全部内容,更多相关leetCode内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部