lintcode|lintcode 172. Remove Element
【lintcode|lintcode 172. Remove Element】难度:
1. Description
文章图片
172. Remove Element 2. Solution
- python
用list的remove
函数
class Solution:
"""
@param: A: A list of integers
@param: elem: An integer
@return: The new length after remove
"""
def removeElement(self, A, elem):
# write your code here
for i in range(A.count(elem)):
A.remove(elem)
return len(A)
不用list的
remove
函数class Solution:
"""
@param: A: A list of integers
@param: elem: An integer
@return: The new length after remove
"""
def removeElement(self, A, elem):
# write your code here
j = len(A)-1
for i in range(len(A)-1, -1, -1):
if A[i] == elem:
A[j], A[i] = A[i], A[j]
j -= 1
return j+1
3. Reference
- https://www.lintcode.com/problem/remove-element/description
推荐阅读
- LintCode|LintCode 545 [Top k Largest Number II]
- vue.js|vue.js window.removeEventListener 移除
- linux|linux yum安装 php7.2
- Codeforces|Codeforces Round #605 (Div. 3) D. Remove One Element
- 快乐数|快乐数 (lintcode:happy-number)
- Android开发-RecyclerView-AndroidStudio(四)属性动画(1)RemoveDuration
- LintCode【简单】60. 搜索插入位置 。代码及思路
- QListWidget的RemoveItemWidget和TakeItem区别
- my.cnf配置文件
- LeetCode|Lintcode: Serialize and Deserialize Binary Tre & 剑指offer(序列化、反序列化二叉树)