[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 ...
随机推荐
- MUD教程--巫师入门教程3
1. 指令格式为:edit <档名>,只加文件名,默认为当前目录,加here,表示编辑你当前所处的房间, 回车后即进入线上编辑系统. 2. 如果这是一个已经有的档案,你可以使用 z 或 Z ...
- CI的扩展机制
CI的扩展机制 在熟悉了CI的源码之后,它的简单明了的代码风格很有趣,这篇文章看看在CI是如何实现扩展的. 扩展包 扩展是为了完成特定的功能,在CI中,扩展包的开发只能在application/lib ...
- LGA(land grid array)
产品应用 1.射频功放 2.加速度传感器 3.地磁传感器 可靠性 Reliability 高压蒸煮 PCT 121℃,100%RH,2atm,96hrs 高低温循环 TCT -55℃(15min)~1 ...
- Android Studio下载安装及配置图文教程
原文 http://jingyan.baidu.com/article/9c69d48f56835e13c9024e95.html AndroidStudio下载地址:https://develope ...
- Painting Storages(ZOJ)
There is a straight highway with N storages alongside it labeled by 1,2,3,...,N. Bob asks you to pai ...
- MySQL优化GROUP BY-松散索引扫描与紧凑索引扫描
满足GROUP BY子句的最一般的方法是扫描整个表并创建一个新的临时表,表中每个组的所有行应为连续的,然后使用该临时表来找到组并应用累积函数(如果有).在某些情况中,MySQL能够做得更好,即通过索引 ...
- setInterval和setTimeout的使用区别
setTimeout和setInterval的使用 这两个方法都可以用来实现在一个固定时间段之后去执行JavaScript.不过两者各有各的应用场景. 方 法 实际上,setTimeout和setIn ...
- XCode工程中ARC模式与非ARC模式共用(转)
Xcode 项目中经常会融合一些老的代码,它们可能采用非ARC的模式.混合编译时,就会碰到编译出错的情况. 如何共用ARC模式和非ARC模式呢? XCode除了提供整个项目是否使用ARC模式的选择外, ...
- Android-Tab单选控件
今天看到项目中有一个控件写得很美丽,据说是github上开源的控件,地址没找到,例如以下图所看到的,很常见的效果,几个tab页面来回切换: 转载请标明出处:http://blog.csdn.net/g ...
- Timeout expired 超时时间已到. 达到了最大池大小 错误及Max Pool Size设置
参考数据库链接串: <add key="data" value="server=192.168.1.123; Port=3306; uid=root; pwd=ro ...