leetcode27
2016-06-01 15:35
295 查看
Remove Element
*Given an array and a value, 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 in place with constant memory.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
Example:
Given input array nums = [3,2,2,3], val = 3
Your function should return length = 2, with the first two elements of nums being 2.*
方法1:
方法2:
*Given an array and a value, 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 in place with constant memory.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
Example:
Given input array nums = [3,2,2,3], val = 3
Your function should return length = 2, with the first two elements of nums being 2.*
方法1:
class Solution(object): def removeElement(self,nums, val): while 1: try: nums.pop(nums.index(val)) except: return len(nums)
方法2:
class Solution(object): def removeElement(self,nums, val): i=0 for j in range(len(nums)): if nums[i]==val: nums.pop(i) else: i+=1 return len(nums)
相关文章推荐
- Android.mk 各属性参数详解
- JavaScript数据类型、类与对象
- 83、myeclipse 中 svn 更新 提交 同步资源库 详细解释下他们的功能
- 利用.dSYM和.app文件准确定位Crash位置
- 微服务实践(五):微服务的事件驱动数据管理
- mongodb学习笔记
- Java之序列化SerialVersionUID的作用
- 运放的参数和选择
- 不用float也可以让div横向显示
- 购买书籍的最小价格
- [转]在 .NET 中远程请求 https 内容时,发生错误:根据验证过程,远程证书无效
- CentOS 7下ionic build android 出错 Execution failed for task ':processDebugResources'.
- java基础第十三天_IO
- 微服务实战(四):服务发现的可行方案以及实践案例
- Visio无法复制文本框,内部错误1021
- 关于C语言结构体偏移的一点思考
- 线程同步中的“生产者和消费者”模式
- Xcode 工程文件打不开, cannot be opened because the project
- 经典的28理论
- 4 file和wc命令