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)

最新文章

  1. Permutation Sequence
  2. 一个div,包含两个div,调整文字位置和div平均分布
  3. FW Docker为容器分配指定物理网段的静态IP
  4. JavaSE的知识
  5. memcached学习(二)
  6. 【原】个人对win7开机黑屏只有鼠标排障总结
  7. 使用eclipse搭建嵌入式开发环境
  8. 基于 Quartz 开发企业级任务调度应用--转
  9. CodeForces 150B- Quantity of Strings 推算..
  10. WPF之DataGrid的LoadingRow事件
  11. Eclipse导入项目常见问题----jdk版本问题(有个红色感叹号)01
  12. tomcat中使用mysql连接池的配置
  13. June 16. 2018, Week 24th. Saturday
  14. 如何生成添加前缀的顺序DIV
  15. Vue 旅游网首页开发1-工具安装及码云使用
  16. RESET MASTER和RESET SLAVE使用场景和说明,以及清除主从同步关系
  17. struts-config.xml配置详解
  18. 图的基本算法(BFS和DFS)
  19. ACM1001:Sum Problem
  20. Python+Django+SAE系列教程12-----配置MySQL数据库

热门文章

  1. [Xcode 实际操作]五、使用表格-(8)自定义UITableView单元格Accessory样式(附件图标)
  2. 剑指Offer的学习笔记(C#篇)-- 树的子结构
  3. Mysql深入理解(1)
  4. jsp学习与提高(四)——JSP Session 处理
  5. java基础第十一篇之Date、Math、自动装箱和拆箱
  6. Hexo - 记录一次Pages服务部署失败的原因
  7. 《SQL 进阶教程》case :用一条 SQL 语句进行不同条件的统计
  8. CodeForces - 361A-Levko and Table (思维)
  9. PartTime_网址_内
  10. B. Batch Sort