LeetCode——Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array A = [1,1,2]
,
Your function should return length = 2
, and A is now [1,2]
.
今天心情不好,啥都不想写,题目很简单,见代码。
1 public int removeDuplicates(int[] A) { 2 int len = 0; 3 int temp; 4 for (int i = 0; i < A.length; ) { 5 temp = A[i]; 6 while (A[i] == temp) { 7 i++; 8 if (i == A.length) 9 break; 10 } 11 A[len] = temp; 12 len++; 13 } 14 return len; 15 }