删除排序数组中的重复项再练习
package com.m.algorithm;
import java.util.Arrays;
import java.util.Random;
public class Test {
//1、删除排序数组中的元素
public static int test(int [] nums){
int n = nums.length;
if(n == 0) {
return 0;
}
int index = 1;
for (int i = 1; i < n; i++) {
if(nums[i] == nums[i-1]){
continue;
//跳出本次for循环,以下代码不执行
}
nums[index] = nums[i];
index++;
}
return index;
}
public static void main(String[] args) {
int n = 10;
int [] arr = new int[n];
int index = 0;
for (int i = 0; i < arr.length; i++) {
arr[i] = index;
if(i == 4 || i == 6) {
continue;
}
index+=2;
}
System.out.println("before:"+Arrays.toString(arr));
n = test(arr);
System.out.println(n);
arr = Arrays.copyOf(arr, n);
System.out.println("after:"+Arrays.toString(arr));
}
}
before:[0, 2, 4, 6, 8, 8, 10, 10, 12, 14]
8
after:[0, 2, 4, 6, 8, 10, 12, 14]