移除元素这道题有很多种做法。也要考虑很多种情况。
package bad;
public class removeElement {
public int removeE(int[] nums, int val) {
int ans = 0;
for (int num : nums) {//使用遍历
if (num != val) {
nums[ans] = num;
ans++;
}
}
return ans;
}
public static void main(String[] args) {
removeElement r = new removeElement();
int[] nums={0,1,2,3,4,3,6,3,};
int val=3;
int length=r.removeE(nums,val);//注意removeE
for(int i=0;i<length;i++){
System.out.print(nums[i]+" ");
}
}
}
老鸭子,有误或有补充的地方喊我。