额,我把题目调成了英文模式,没怎么看明白,哈哈...又被输出结果案例误导了。
个人微改:
import java.util.Scanner; public class removeDuplicationFromSortedArray { ?? ?public static void main(String args[]) { ?? ??? ?System.out.println("please enter the capcity of your array :"); ?? ??? ?Scanner sc = new Scanner(System.in); ?? ??? ?int m = sc.nextInt(); ?? ??? ?int a[] = new int[m]; ?? ??? ?System.out.println("please enter the valus of your array :"); ?? ??? ?for(int i=0;i<m;i++) { ?? ??? ??? ?a[i]=sc.nextInt(); ?? ??? ?} ?? ??? ?int q = remove(a); ?? ??? ?System.out.println("the number of digits that do not repeat :"); ?? ??? ?System.out.println(q); ?? ??? ?System.out.println("the new array looks like this :"); ?? ??? ?for(int i=0;i<q;i++) { ?? ??? ??? ?System.out.print(a[i]+" "); ?? ??? ?} ?? ??? ?sc.close(); ?? ?} ?? ? ?? ?static int remove(int nums[]) { ?? ??? ?int m = nums.length; ?? ??? ?if(m==0) { ?? ??? ??? ?return 0; ?? ??? ?} ?? ??? ?int fast=0,slow=0; ?? ??? ?while(fast<m-1) { ?? ??? ??? ?if(nums[fast+1]!=nums[fast]) { ?? ??? ??? ??? ?nums[slow+1]=nums[fast+1]; ?? ??? ??? ??? ?++slow; ?? ??? ??? ?} ?? ??? ??? ?++fast; ?? ??? ?} ?? ??? ?return slow+1; ?? ?} }
测试结果:
please enter the capcity of your array : 10 please enter the valus of your array : 0 0 1 1 1 2 2 3 3 4 the number of digits that do not repeat : 5 the new array looks like this : 0 1 2 3 4?
|