27. Remove Element@python
2024-09-04 12:57:00
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.
题目地址: Remove Element
难度: Easy
题意: 删除指定值,并将其他元素移动前面
思路:
遍历,双指针
代码:
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
n = len(nums)
i, j = 0, n-1
while i <= j:
if nums[i] == val:
nums[i], nums[j] = nums[j], nums[i]
j -= 1
else:
i += 1
return i
时间复杂度: O(n)
空间复杂度: O(1)
最新文章
- Permutation Sequence
- 一个div,包含两个div,调整文字位置和div平均分布
- FW Docker为容器分配指定物理网段的静态IP
- JavaSE的知识
- memcached学习(二)
- 【原】个人对win7开机黑屏只有鼠标排障总结
- 使用eclipse搭建嵌入式开发环境
- 基于 Quartz 开发企业级任务调度应用--转
- CodeForces 150B- Quantity of Strings 推算..
- WPF之DataGrid的LoadingRow事件
- Eclipse导入项目常见问题----jdk版本问题(有个红色感叹号)01
- tomcat中使用mysql连接池的配置
- June 16. 2018, Week 24th. Saturday
- 如何生成添加前缀的顺序DIV
- Vue 旅游网首页开发1-工具安装及码云使用
- RESET MASTER和RESET SLAVE使用场景和说明,以及清除主从同步关系
- struts-config.xml配置详解
- 图的基本算法(BFS和DFS)
- ACM1001:Sum Problem
- Python+Django+SAE系列教程12-----配置MySQL数据库
热门文章
- [Xcode 实际操作]五、使用表格-(8)自定义UITableView单元格Accessory样式(附件图标)
- 剑指Offer的学习笔记(C#篇)-- 树的子结构
- Mysql深入理解(1)
- jsp学习与提高(四)——JSP Session 处理
- java基础第十一篇之Date、Math、自动装箱和拆箱
- Hexo - 记录一次Pages服务部署失败的原因
- 《SQL 进阶教程》case :用一条 SQL 语句进行不同条件的统计
- CodeForces - 361A-Levko and Table (思维)
- PartTime_网址_内
- B. Batch Sort