力扣——remove element(删除元素) python实现
2024-10-07 17:02:27
题目描述:
中文:
给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度。
不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。
英文:
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.
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
while val in nums:
nums.remove(val)
return len(nums)
题目来源:力扣
最新文章
- 怎样实现ZBrush中的智能对称
- bzoj1029 [JSOI2007]建筑抢修
- 解决 Tomcat 无法绑定 80 端口的问题,以及 Tomcat 配置虚拟目录、二级域名等
- Owin中间件搭建OAuth2.0认证授权服务体会
- 批处理安装APK
- session共享,格式json,php不能简单的设置session.serialize_handler=json,目前只有php,wddx(xml),安装扩展后还有igbinary(二进制)
- Visual Studio 2010下载 + 附破解方法
- git使用的常见命令(一)
- Codevs 4768 跳石头 NOIP2015 DAY2 T1
- NetFlow
- OSX: 10.9的SMB网络共享连接可能破坏其权限设置
- 使用最小堆优化Dijkstra算法
- [SCOI2007]最大土地面积
- keepalived双机热备nginx
- <;二>;ELK-6.5.3学习笔记–使用rsyslog传输管理nginx日志
- windows server 2012 流媒体服务器搭建(直播与点播)
- [原]Jenkins(七)---jenkins项目编译测试发布由maven构建的web项目
- 雷林鹏分享:XML 编辑器
- 将远程mysql服务器数据导出 csv 并发送到我的本机
- 4. mysql 查看数据库中所有表的记录数
热门文章
- No enclosing instance of type TestGson is accessible. Must qualify the allocation with an enclosing instance of type TestGson (e.g. x.new A() where x is an instance of TestGson).
- java 时间戳转为时间
- 【HDU6701】Make Rounddog Happy【权值线段树+双向单调队列】
- (转)堆和栈的概念和区别 HeapOutOfMemory和StackOverflow解释
- JSP 获取访问者真正的IP地址
- JAVA 大数开方模板
- PHP连接FTP服务的简单实现
- codeforces 559D Randomizer
- Nuget-Swagger-Swashbuckle:Swashbuckle
- Linux随笔 - linux 多个会话同时执行命令后history记录不全的解决方案【转载】