1.删除有序数组中的重复项
class Solution {
public int removeDuplicates(int[] nums) {
int fast = 1;
int slow = 1;
for(;fast<nums.length;fast++) {
if( nums[fast] !=nums[fast-1] ) {
nums[slow] = nums[fast];
slow++;
}
}
return slow;
}
}
2.移除元素
class Solution {
public int removeElement(int[] nums, int val) {
int len = nums.length;
int slow = 0;
int fast = 0;
for(fast=0;fast<len;fast++) {
if( nums[fast] !=val ) {
nums[slow] = nums[fast];
slow++;
}
}
return slow;
}
}