删除有序数组中的重复项
package Algorithms; /** * @author : zhang * @version : 1.0 * @date : Create in 2021/8/1 * @description : */ public class RemoveDuplicates { public static void main(String[] args) { int[] nums = {1,1,2,2,3,4,4,5,6}; int len = removeDuplicates(nums); System.out.println("数组的长度为:"+len); for (int i = 0; i < len; i++) { //不需要考虑数组中超出新长度后面的元素 System.out.println(nums[i]); } } public static int removeDuplicates(int[] nums) { if (nums == null || nums.length == 0) return 0; int p = 0; int q = 1; while (q < nums.length) { if (nums[p] != nums[q]) { nums[p + 1] = nums[q]; p++; } q++; } return p + 1; } }