27. 移除元素
题链:https://leetcode.cn/problems/remove-element/
代码:
class Solution {
public int removeElement(int[] nums, int val) {
int size = nums.length;
for (int i = 0; i < size; i++) {
if (nums[i] == val){
for (int j = i+1; j < size; j++) {
nums[j-1] = nums[j];
}
i--;
size--;
}
}
return size;
}
}
时间复杂度:O(n^2)
空间复杂度:O(1)