LeetCode:Remove Duplicates from Sorted Array

来源:互联网 发布:mac系统重装的网络加速 编辑:程序博客网 时间:2024/06/05 09:54

1.

class Solution {public:    int removeDuplicates(int A[], int n) {    if(n==0)        return 0;    int index = 0;    for(int i = 1;i<n;i++){        if(A[index]!=A[i])            A[++index] = A[i];}        return index+1;    }};

2.

class Solution {    public:    int removeDuplicates(int A[], int n) {          if (n == 0) return 0;                         int slow(1), fast(1);                while(fast < n){                        while(fast < n && A[fast] == A[fast-1])                  fast++;                        if (fast < n)                                A[slow++] = A[fast++];                }                             return slow;        }};

思路都是一样的
一直往后找不等于前面的数 然后赋值

0 0