simple 27.移除元素
分析: 根据提示,int[] nums类似于C中的引用,而且题目明确不需要考虑超出返回长度的溢出部分,那么代码这么写即可:
public class removeElement {
public int removeElement(int[] nums, int val) {
int length = 0; // 记录返回的长度
int j = 0; // 把不等于val的值往前移
for(int i = 0; i < nums.length; i++) {
if (nums[i] != val) {
nums[j] = nums[i];
length++;
j++;
}
}
return length;
}