[Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 27: Remove Element
https://oj.leetcode.com/problems/remove-element/ 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. ===Comments by Dabay===
一次循环,两个指针,一个指向插入的位置,另外一个一直往前面走。
如果二号指针的数就是需要删除的数,二号指针继续走。
如果不是要删除的数,把二号指针指向的数移到一号指针的位置上,然后两个指针继续走。
最后跟新数组,返回长度。
''' class Solution:
# @param A a list of integers
# @param elem an integer, value need to be removed
# @return an integer
def removeElement(self, A, elem):
i = j = 0
while j < len(A):
if A[j] != elem:
A[i] = A[j]
i += 1
j += 1
A = A[:i]
return len(A) def main():
sol = Solution()
print sol.removeElement([1,1,2], 1) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)
[Leetcode][Python]27: Remove Element的更多相关文章
- C# 写 LeetCode easy #27 Remove Element
27. Remove Element Given an array nums and a value val, remove all instances of that value in-place ...
- 【LeetCode】27. Remove Element (2 solutions)
Remove Element Given an array and a value, remove all instances of that value in place and return th ...
- 【LeetCode】27. Remove Element 解题报告(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 记录起始位置 日期 题目地址:https:/ ...
- 【LeetCode】27 - Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- 【一天一道LeetCode】#27. Remove Element
一天一道LeetCode系列 (一)题目 Given an array and a value, remove all instances of that value in place and ret ...
- Leetcode No.27 Remove Element(c++实现)
1. 题目 1.1 英文题目 Given an integer array nums and an integer val, remove all occurrences of val in nums ...
- LeetCode OJ 27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. D ...
- LeetCode:27. Remove Element(Easy)
1. 原题链接 https://leetcode.com/problems/remove-element/description/ 2. 题目要求 给定一个整数数组 nums[ ] 和一个整数 val ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- easyui跨iframe属性datagrid
1.问题 如何刷新easyui父级tab页中iframe嵌套页中的datagrid? 2.解决方法 (1) parent.$("iframe[title='tabtitle']") ...
- Linux查看所有用户用什么命令1
用过Linux系统的人都知道,Linux系统查看用户不是会Windows那样,鼠标右键看我的电脑属性,然后看计算机用户和组即可. 那么Linux操作系统里查看所有用户该怎么办呢?用命令.其实用命令 ...
- offsetParent和parentNode区别
offsetParent用的最普遍的就是来计算元素在页面中的位置,前面的日志理讲了 通过getBoundingClientRect() 来获取页面中元素的位置,不过这只支持最新的浏览器,如果要兼容像O ...
- Aix 光盘软件包安装
1, mount -rv cdrfs /dev/cd0 /mnt (Aix 光盘挂载)2,fuser -kxcu /cdrom && unmount /mnt && u ...
- Cannot retrieve metalink for repository: epel.
Error: Cannot retrieve metalink for repository: epel. Please verify its path and ...
- poj1144Network(无向图割点数)
题目请戳这里 题目大意:给一张无向图,求割点数量. 题目分析:tarjan算法求割点.关于无向图割点,这里说的很清楚了.直接建图跑一遍tarjan算法即可. 详情请见代码: #include < ...
- SRM 581 D2 L2:SurveillanceSystem,重叠度
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12588 在判断 ‘+’ 的时候使用了 重叠度 的概念,跟一般的 ...
- Python学习笔记3-Python元组、数组、字典集合的操作
在Python中数组中的每一项可以是不同的数据类型 元组:只能读不能写的数组 aTuple=(1,'abc','tmc',79.0,False) print aTuple[1:3] print typ ...
- poj 3764 The xor-longest Path(字典树)
题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值.找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每一个节点到根 ...
- ASP.Net连接SQLServer 连接字符串
引用命名空间 using System.Data; using System.Data.SqlClient; 连接字符串 // windows 验证方式数据库中存在单一实例 string connec ...