leetcode: Remove Duplicates from Sorted Array

来源:互联网 发布:3g网络网速 编辑:程序博客网 时间:2024/05/20 16:10

扫描数组,用一个指针维护去重后的的数组。  这样在不需要额外数组空间的情况下可以获得去重数组的长度并且排列在A的前端。


public class Solution {    public int removeDuplicates(int[] A) {        if(A==null||A.length==0)        {            return 0;        }        int len = A.length;        int res=1;        for( int i=1;i<len;i++ )        {            if(A[i]==A[res-1])            {                continue;            }            A[res]=A[i];            res++;        }        return res;    }}


0 0
原创粉丝点击