【leetcode】Remove Element (easy)

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.

思路:

s记录下一个判断位置, e记录结束位置,把前面的待排除元素与后面要保留的元素互换。

int removeElement(int A[], int n, int elem) {
    int s = 0, e = n - 1;
    while(s <= e)
    {
        if(A[e] == elem) //末尾待删 直接删除
        {
            e--;
            continue;
        }
        if(A[s] == elem)
        {
            A[s++] = A[e--];
        }
        else
        {
            s++;
        }
    }
    return e + 1;
}
时间: 2024-08-02 13:38:51

【leetcode】Remove Element (easy)的相关文章

【leetcode】Happy Number(easy)

Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the

【leetcode】Count Primes(easy)

Count the number of prime numbers less than a non-negative number, n 思路:数质数的个数 开始写了个蛮力的,存储已有质数,判断新数字是否可以整除已有质数.然后妥妥的超时了(⊙v⊙). 看提示,发现有个Eratosthenes算法找质数的,说白了就是给所有的数字一个标记,把质数的整数倍标为false,那么下一个没被标为false的数字就是下一个质数. int countPrimes(int n) { if(n <= 2) retu

【leetcode】Same Tree(easy)

Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 思路:太简单! bool isSameTree(TreeNode *p, TreeNode *q) { if(p == NULL &

【LeetCode】Remove Element

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. 解法一:使用vector记录每个elem的位置,介于这些位置之间的元素就可以前移相应

LeetCode:27. Remove Element(Easy)

1. 原题链接 https://leetcode.com/problems/remove-element/description/ 2. 题目要求 给定一个整数数组 nums[ ] 和一个整数 val,删除数组中与val相同的元素,并返回删除后的数组长度 注意:不能定义新的数组,只能使用O(1)空间大小 3. 解题思路 遍历一次,将每个元素与给定的value进行比较,不同则给nums[count++]赋予当前元素的值:相同则直接跳过,最后返回count,即为删除后数组的长度. 4. 代码实现 p

【leetcode】Reverse Integer(middle)☆

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出的方法 ①用数据类型转换long  或 long long ②在每次循环时先保存下数字变化之前的值,处理后单步恢复看是否相等 (比③好) ③整体恢复,看数字是否相等. 思路:注意30000这样以0结尾的数字,注意越界时返回0. 我检查越界是通过把翻转的数字再翻转回去,看是否相等. int rever

【LeetCode】Reconstruct Itinerary(332)

1. Description Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus, the itinerary must begin with JFK. No

【leetcode】Word Break (middle)

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true because &

【leetcode】House Robber (middle)

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will autom