27. 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.
题意,给定一个数组和数值,要求去除数组中的与给定数值相等的数,然后返回现在数组的长度。
刚开始,没理解去除是什么意思,艹, 不是删掉啊……
这样原来的数组就变的不是原来的数组的,是删减后的数组,而后返回其长度。
public class Solution {
public int removeElement(int[] nums, int val) {
int j=0;
for(int i = 0; i < nums.length ; i++){
if(nums[i]-val!=0){
nums[j++]=nums[i];
}
}
return j;
}
}