Leetcode 挑战 Day 15 [27. Remove Element]

27. Remove Element


今天我们一起挑战leetcode第27题Remove Element!

题目


Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The relative order of the elements may be changed.

Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.

Return k after placing the final result in the first k slots of nums.

Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.

Custom Judge:

The judge will test your solution with the following code:

int[] nums = [...]; // Input array
int val = ...; // Value to remove
int[] expectedNums = [...]; // The expected answer with correct length.
// It is sorted with no values equaling val.

int k = removeElement(nums, val); // Calls your implementation

assert k == expectedNums.length;
sort(nums, 0, k); // Sort the first k elements of nums
for (int i = 0; i < actualLength; i++) {
assert nums[i] == expectedNums[i];
}
If all assertions pass, then your solution will be accepted.

Example 1:
Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,,]
Explanation: Your function should return k = 2, with the first two elements of nums >being 2.
It does not matter what you leave beyond the returned k (hence they are underscores).

Example 2:
Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3,,,_]
Explanation: Your function should return k = 5, with the first five elements of nums >containing 0, 0, 1, 3, and 4.
Note that the five elements can be returned in any order.
It does not matter what you leave beyond the returned k (hence they are underscores).

今天题目看似很长,但其实要我们做的事情也很简单,就是要我们把题目指定的数字val,从题目给我们的整数阵列中移除,并把剩下来的整数移到阵列头的位置,我们并不用回传阵列,只要回传剩下来的整数总数目。此外,题目希望我们最多利用额外的big O(1)空间复杂度就完成这一题。

ForLoop


这题其实可以用简单的for回圈就能够完成,只要我们走访一次题目给我们的阵列,遇到不属於题目指定要删除的元素,我们就把他往前面放,并且建立一变数count,把他加一,这样我们下次就能知道要放在下一个位置。而且在走访完全部回圈後,我们也可以简单知道count就是我们要回传的答案。

以下是python的程序码

class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        count = 0
        for i in range(len(nums)):
            if nums[i] != val:
                nums[count] = nums[i]
                count += 1
        return count

<<:  机器学习:模型训练架构

>>:  [Day5] Face Detection - 使用Google ML Kit (Android)

Day12 Let's ODOO: Security(1) Access right

藉由ODOO的security,进行对model的权限设定,我们今天来写一个student 权限的范...

Day1 研究AR的起因&初心(刚出新手村的萌新)

选择研究/探讨AR相关技术是因为希望未来可以做出一些很酷的东西,像Pokemon GO或是游戏王卡的...

Day 12 : 案例分享(4.1) 签核与费用模组 - 费用申请流程

案例说明及适用场景 初期要上线使用odoo我最推荐的是费用核销模组 一方面可免除销售、采购、库存等较...

冒险村16 - customize breadcurmb

延续上篇 customize tooltips with data attribute 後,另外也来...

[day30][後记] 什麽都略懂一点,生活更多彩一些。

同步发表到驴形笔记 什麽都略懂一点,生活更多彩一些。 经过这ㄧ系列跑下来,应该有摸索到各式各样的新...