欢迎来我Python萌新的小站

[LEEDCode][027][简单] – 删除元素(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 elements can be changed. It doesn’t matter what you leave beyond the new length.

给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度。

不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。

元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。

示例 1:

示例 2:

说明:

为什么返回数值是整数,但输出的答案是数组呢?

请注意,输入数组是以“引用”方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。

你可以想象内部操作如下:

解答

自测用例

执行结果

提交时间 状态 执行用时 语言
几秒前 通过 48 ms python3
喜欢 (1)分享 (0)
发表我的评论
取消评论
友情提示:插入代码时请使用:  <pre>要插入的代码</pre>   以达到最佳效果!

表情     8 + 8 = ? (必填)

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址