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]

 

leetcode--Remove Duplicates from Sorted Array
 1 public class Solution {
 2     public int removeDuplicates(int[] A) {
 3         int length = 0;
 4         int j = 0;
 5         if(A.length > 0){
 6             for(int i = 1; i < A.length; ++i){
 7                 if(A[i] != A[j]){
 8                     A[j + 1] = A[i];
 9                     ++j;
10                 }                    
11             }
12         length = j + 1;
13         }
14         return length;
15     }
16 }
leetcode--Remove Duplicates from Sorted Array

leetcode--Remove Duplicates from Sorted Array

上一篇:Win下建立Socket时注意事项


下一篇:ubuntu grub