/**
* 删除排序数组中的重复项,返回新数组长度
*/
public class DeleteRepeat {
public static void main(String[] args) {
int[] arr = {1, 3, 3, 5, 7, 7};
System.out.println("删除重复项后数组长度为:"+Solution(arr));
}
public static int Solution(int[] arr){// 思路:双指针
int index = 0;
for (int j = 1; j < arr.length; j++) {
if (arr[index] != arr[j]){
index++;
arr[index] = arr[j];
}
}
return index+1;
}
}
java实现“删除排序数组中的重复项”
最新推荐文章于 2023-06-30 21:38:53 发布