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. 代码实现

public class RemoveElement27 {
    public static void main(String[] args) {
        int[] nums = {2, 34, 5, 67, 89, 5, 4};
        System.out.println(removeElement(nums, 5));
    }

    public static int removeElement(int[] nums, int val) {
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != val)
                nums[count++] = nums[i];
        }
        return count;
    }
}

  

原文地址:https://www.cnblogs.com/huiAlex/p/leetcode.html

时间: 2024-10-10 12:14:44

LeetCode:27. Remove Element(Easy)的相关文章

27. Remove Element(js)

27. Remove Element Given an array nums and a value val, remove all instances of that value in-place and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memor

【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 removeE

LeetCode:20. Valid Parentheses(Easy)

1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')', '{', '}', '[' 和 ']'. 合法:形如"()[]"."{[()]}" 不合法:形如"([)]"."[[((" 判断所给字符串s是否合法. 3. 解题思路 对字符串s转换成字符数字进行遍历. 利用栈,遇到左半边字符:'

27. Remove Element【easy】

27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be ch

LeetCode解题思路:27. Remove Element

Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be changed. It doesn't matter w

LeetCode:39. Combination Sum(Medium)

1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值target,找出数组中累加之后等于target的所有元素组合 注意:(1)数组中的每一个元素可以重复用:(2)数组中不存在重复元素:(3)数组中都是正整数 3. 解题思路 采用迭代的方法检验所有元素组合 4. 代码实现 1 import java.util.ArrayList; 2 import

[Leetcode][Python]27: Remove Element

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 27: Remove Elementhttps://oj.leetcode.com/problems/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 b

LeetCode 27 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 lengt

【Divide and Conquer】169. Majority Element(easy)

#Week_1# #From LeetCode# Description: Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times. You may assume that the array is non-empty and the majority element always exist in t