标签:style blog class code java tar javascript color int art http
没有想通为什么这个简单的问题竟然不是那么简单,太小看它了,以下是两个别人的很不错的solution:
Solution1:
1 public class Solution { 2 public int removeDuplicates(int[] A) { 3 // Start typing your Java solution below 4 // DO NOT write main() function 5 int i=1; 6 7 while(i<A.length){ 8 if(A[i]>A[i-1]) 9 i++; 10 else{ 11 if(A[i-1]>=A[A.length-1]) 12 return i; 13 14 for(int j=i+1;j<A.length;j++) 15 if(A[j]>A[i-1]){ 16 swap(A,i,j); 17 break; 18 } 19 } 20 } 21 return A.length; 22 } 23 24 public void swap(int[] A,int i,int j){ 25 int temp = A[i]; 26 A[i] = A[j]; 27 A[j] = temp; 28 } 29 }
Solution 2:
1 public class Solution { 2 public int removeDuplicates(int[] A) { 3 if (A.length == 0) { 4 return 0; 5 } 6 if (A.length == 1) { 7 return 1; 8 } 9 int counter = 0; 10 for (int i = 1; i <= A.length - 1; i++) { 11 if (A[i] != A[i - 1]) { 12 A[counter] = A[i - 1]; 13 counter++; 14 } 15 } 16 A[counter] = A[A.length - 1]; 17 counter++; 18 A = Arrays.copyOf(A, counter); 19 return counter; 20 } 21 }
Remove Duplicates from Sorted Array,码迷,mamicode.com
Remove Duplicates from Sorted Array
标签:style blog class code java tar javascript color int art http
原文地址:http://www.cnblogs.com/EdwardLiu/p/3702707.html