[LeetCode] 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.

这题做下来感觉技巧性比较强,解出第一种解法以后我又尝试了另外两种解法,一个比一个简单。。。我一开始却折腾出了最晦涩的解法,可见一开始并没有对题目有深刻的理解。

首先说说第一种解法,思路就是:维护一个表示“下一个交换位置”的指针ex,初始为-1。遍历数组,当找到第一个elem的位置时,将ex指向他,

然后程序要做的就是在遍历过程中检查ex是否存在,存在的话就把当前数挪到ex处,同时把ex向前移动一位。

但是要注意一点是,当再次遇到elem时ex是不更新的。(也就是ex只是在elem第一次出现的时候为其赋值,然后就只用递增他就行了)

int removeElement(int A[], int n, int elem) {
    int ex = -1;
    int len = n;
    for (int i = 0; i < n; i++) {
        if (A[i] == elem) {
            len--;
            if (ex < 0)
                ex = i;
        }
        else if (ex >= 0) {
            A[ex] = A[i];
            ex++;
        }
    }
    return len;
}

这个解法虽然略显晦涩,但好处就是时间复杂度是O(n),空间复杂度是O(1) ,算是一个比较理想的结果。

解法二:空间复杂度O(n)

使用额外的空间可以使思路变得简单,要做的就是从A里面逐一拷贝元素到aux里,其中跳过elem就行了。缺点就是提升了空间复杂度,还有到最后需要把aux

的值在重新赋值给A,这样时间复杂度也多了一倍到达了theta(2n),空间复杂度为O(n)

int removeElement2(int A[], int n, int elem) {
    int len = n;
    int aux[n];
    int k = 0;
    for (int i = 0; i < n; i++) {
        if (A[i] == elem) {
            len--;
        }
        else {
            aux[k++] = A[i];
        }
    }

    for (int i = 0; i < len; i++) {
        A[i] = aux[i];
    }
    return len;
}

解法三:认真观察删除操作的性质会发现,删除一个元素不过就是把他后面的元素向前移动一位,那删除两个不就是移动两位,三个就是三位。。。以此类推。

所以我们只用在遍历数组的时候跟踪删除的个数n,然后同时将元素向后挪动n位就可以了,就这么简单。

int removeElement3(int A[], int n, int elem) {
    int l = 0;
    for (int i = 0; i < n; i++) {
        if (A[i] == elem) {
            l++;
        }
        else if (l > 0){
            A[i-l] = A[i];
        }
    }
    return n-l;
}
时间: 2024-08-28 23:25:52

[LeetCode] Remove Element (三种解法)的相关文章

]Leetcode]-[Reorder List ]-三种解法

Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. 题目的意思就是,给定一个链表,从两头开始链接, 比如1-2-3-4-5-6,最开始取两头,组成1-

LeetCode算法题-First Bad Version(Java实现-三种解法)

这是悦乐书的第200次更新,第210篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第66题(顺位题号是278).您是产品经理,目前领导团队开发新产品.不幸的是,您产品的最新版本未通过质量检查.由于每个版本都是基于以前的版本开发的,因此坏版本之后的所有版本也是坏的. 假设您有n个版本[1,2,...,n]并且您想找出第一个坏的版本,这会导致以下所有版本都不好.您将获得一个API bool isBadVersion(版本),它将返回版本是否错误. 实现一个函数来查找第一

LeetCode算法题-Move Zeroes(Java实现-三种解法)

这是悦乐书的第201次更新,第211篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第67题(顺位题号是283).给定一个数组nums,写一个函数将所有0移动到它的末尾,同时保持非零元素的相对顺序.例如: 输入:[0,1,0,3,12] 输出:[1,3,12,0,0] 注意: 您必须在不制作数组副本的情况下就地执行此操作. 最小化操作总数. 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试. 02

UVALive 6257 Chemist&#39;s vows --一道题的三种解法(模拟,DFS,DP)

题意:给一个元素周期表的元素符号(114种),再给一个串,问这个串能否有这些元素符号组成(全为小写). 解法1:动态规划 定义:dp[i]表示到 i 这个字符为止,能否有元素周期表里的符号构成. 则有转移方程:dp[i] = (dp[i-1]&&f(i-1,1)) || (dp[i-2]&&f(i-2,2))     f(i,k):表示从i开始填入k个字符,这k个字符在不在元素周期表中.  dp[0] = 1 代码: //109ms 0KB #include <ios

LeetCode——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. 中文:给定一个数组和一个数值,去除这个数值所有出现位置,并返回新数组的长度. 元素的顺序可以改变.除了新的长度,你

最长单调递增子序列的三种解法

问题描述: 找出由n个数组成的序列的最长单调递增子序列 解法一:转化成LCS问题求解,时间复杂度为O(n*n). 思路:原序列为A,把A按升序排序得到序列B,求出A,B序列的最长公共子序列,即为A的最长单调递增子序列. #include<iostream> #include<algorithm> #include<string> #include<cstdio> using namespace std; //转化成LCS问题,时间复杂度O(n*n) int

C语言--求字符串长度的三种解法

问题: 求一个字符串的三种解法 一.计数的方法 #include<stdio.h> #include<assert.h> int my_strlen( char* str) { int count=0; while (*str) { count++; str++; } return count; } int main(void) { char *arr = "abcef"; int ret = my_strlen(arr); printf("%d\n&

LeetCode: Remove Element [026]

[题目] 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. [题意] 删除数组中指定的值.不关心在新数组的后面即数组尾部留下了什么值. [思路] 思路同Remove

[LeetCode] Remove Element [20]

题目 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. 原题链接(点我) 解题思路 给一个数组和一个数字,移除该数字在数组中所有出现的地方. 这是一个非常简单的题目