• 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;
        }
    };
    
  • 相关阅读:
    Android 表格布局
    Python 字符串操作分类
    设置Safari禁止访问某个网站
    java判断路径是文件夹还是文件
    java上下分页窗口流动布局
    Python获取网页html代码
    一次失败的java Box居中尝试
    装饰器进阶和迭代器
    函数对象补充,包函数与装饰器
    函数对象和名称空间
  • 原文地址:https://www.cnblogs.com/liyangguang1988/p/3954216.html
Copyright © 2020-2023  润新知