27. Remove Element leetcode python 2016 new season
2016-01-12 11:32
387 查看
Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
the problem require constant space .
we need a count to maintain non-duplicate numbers and an index to iterate through class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
nums_length = len(nums)
if nums_length < 2:
return nums_length
count = 1
index = 1
while index < nums_length - 1:
if nums[index] != nums[index]:
nums[count] = nums[index]
count += 1
index += 1
return count
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
the problem require constant space .
we need a count to maintain non-duplicate numbers and an index to iterate through class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
nums_length = len(nums)
if nums_length < 2:
return nums_length
count = 1
index = 1
while index < nums_length - 1:
if nums[index] != nums[index]:
nums[count] = nums[index]
count += 1
index += 1
return count
相关文章推荐
- Python 连接 MySQL数据库
- python数字图像处理(6):图像的批量处理
- python操作MySQL数据库
- python遍历文件夹并实现分级缩进显示
- Python -- str 类
- python中if __name__ == '__main__': 的解析
- python生成随机数组
- Python异常处理
- Python中的method
- Python下划线与命名规范
- Python开发Webservice(SOAP)
- WIN10 64bit python2.7 安装 numpy scipy matplotlib
- 【极客学院】-python学习笔记-3-单线程爬虫 (request安装遇到问题及解决,应用requests提取信息)
- Python系列之 - Collections系列
- 从0到1 python简易计算器
- Cairo Tutorial for Python Programmers
- 堆排序(Python版)
- 直接选择排序(Python版)
- Python标准库00-02
- python学习的一点点心得