LeetCode OJ_题解(python):027-Remove Element 【Array】【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 changed. It doesn‘t matter what you leave beyond the new length.

Example:
Given input array nums = [3,2,2,3]val = 3

Your function should return length = 2, with the first two elements of nums being 2.

给定一个数组和一个数值val,将数组中数值等于val的数去除,返回新数组的长度。不能申请额外空间,超过新数组长度部分忽略。


题目思路:

由于给定的数组已经排序,那么用i,j两个下标,i记录新数组的下标,j是原来数组下标;

如果nums[j] != val,那么nums[i] = nums[j],i,j各+ 1,最后返回 i


代码:

class Solution(object):
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        i = 0
        j = 0
        size = len(nums)
        while j <size:    #  保证循环结束
            if nums[j] == val:     #  若有与val相同的数
                j += 1
            else:
                nums[i] = nums[j]      #  将在比较的数赋给下一个数
                i += 1
                j += 1
        return i
if __name__ == "__main__":
    s = Solution()
    print(s.removeElement(nums =[3, 2, 2, 3], val = 3))
时间: 2024-10-05 05:32:17

LeetCode OJ_题解(python):027-Remove Element 【Array】【Easy】的相关文章

[LeetCode] 027. Remove Element (Easy) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 027. Remove Element (Easy) 链接: 题目:https://oj.leetcode.com/problems/remove-element/ 代码(github):https://github.com/illuz/leetcode 题意: 删除一个数组里值为 elem 的所有数. 分析: 用

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 027 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. 代码如下: class Solution { public: int re

LeetCode OJ_题解(python):001-Two Sum 【Array】【Easy】

题目: Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. 输入一个数组和target,要在一个数组中找到两个数字,其和为t

LeetCode OJ_题解(python):035-Search Insert Position【Array】【Easy】

题目: Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples. [1,3,5,6], 5 → 2[1,3,5,6]

[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】027. 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 mat

Java for LeetCode 027 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. 解题思路: 看代码即可 JAVA实现如下: public int removeElement(int[] nums

【LeetCode每天一题】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 memory. The order of elem