标签:元素 remove article java 方法 step for class rac
Remove Element
The order of elements can be changed. It doesn‘t matter what you leave beyond the new length.
思路:此题和26题一脉相承,算法上不难,详细如代码所看到的:
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 (删除元素) 解题思路和方法
标签:元素 remove article java 方法 step for class rac
原文地址:http://www.cnblogs.com/yutingliuyl/p/7040068.html