zoukankan      html  css  js  c++  java
  • 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    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:与前面相同,则删除。关键是移动元素不要出错。(不可取: 1360ms)

    class Solution {
    public:
        int removeDuplicates(int A[], int n) {
            int i = 1;
            while(i < n) {
                if(A[i] == A[i-1]) {
                    int j = i;
                    while(j < n-1) 
                        A[j++] = A[j+1]; 
                    --n; 
                }
                else ++i;
            }
            return n;
        }
    };
    

      方法2:(优)不用移动元素。设置自增变量,如 A 中该元素与前一元素不同,则放入该变量位置,变量增 1 .(132 ms)

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

    Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates": What if duplicates are allowed at most twice?

    For example, Given sorted array A = [1,1,1,2,2,3],

    Your function should return length = 5, and A is now [1,1,2,2,3].

    思路:该位置的前两个重复时,设置标志 repeat = true.

    方法1:(移动元素: 136ms)

    void remove(int A[], int id, int& n) {
        while(id < n-1) A[id++] = A[id+1];
        --n;
    }
    class Solution {
    public:
        int removeDuplicates(int A[], int n) {
            bool repeat = false;
            int i = 1;
            while(i < n) {
                if(repeat && A[i] == A[i-1]) { remove(A, i, n); continue; }
                if(A[i] == A[i-1]) repeat = true;
                else repeat = false;
                ++i;
            }
            return n;
        }
    };
    

     方法2: (优:不移动元素:80ms)

    class Solution {
    public:
        int removeDuplicates(int A[], int n) {
            if(n < 3) return n; 
            bool repeat = false;
            int id = 1;
            for(int i = 1; i < n; ++i) {
                if(repeat && A[i] == A[i-1]) continue; 
                if(A[i] == A[i-1]) { repeat = true; A[id++] = A[i]; }
                else { repeat = false; A[id++] = A[i]; }
            }
            return id;
        }
    };
    

    Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length.

    The order of elements can be changed. It doesn't matter what you leave beyond the new length.

    思路: 同上。

    class Solution {
    public:
        int removeElement(int A[], int n, int elem) {
            int L = 0;
            for(int i = 0; i < n; ++i) {
                if(A[i] == elem) continue;
                A[L++] = A[i];
            }
            return L;
        }
    };
    
  • 相关阅读:
    Ext.form.TextField组件
    provider: SQL Network Interfaces, error: 26 Error Locating Server/Instance Specified解决办法
    SQL Server访问远程数据库和Linked Server
    C#程序实现动态调用DLL的研究
    把DLL文件打包进EXE的技巧
    C#读取媒体信息
    C# 集合类
    Server Application Unavailable 【Failed to execute request because the AppDomain could not be created.】的解决办法
    比较全的字符串验证类
    如何用VS2005制作Web安装程序
  • 原文地址:https://www.cnblogs.com/liyangguang1988/p/3954216.html
Copyright © 2011-2022 走看看