【LeetCode with Python】 Remove Element
2008-09-13 00:43
411 查看
博客域名:http://www.xnerv.wang
原题页面:https://oj.leetcode.com/problems/remove-element/
题目类型:数组
难度评价:★
本文地址:/article/1377561.html
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.
题目已经暗示可以将需要删除的元素移至数组末尾,因此用两个下标m和n,m用来从左至右遍历数组寻找该元素,n从右至左记录可以用来交换的尾部位置。
原题页面:https://oj.leetcode.com/problems/remove-element/
题目类型:数组
难度评价:★
本文地址:/article/1377561.html
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.
题目已经暗示可以将需要删除的元素移至数组末尾,因此用两个下标m和n,m用来从左至右遍历数组寻找该元素,n从右至左记录可以用来交换的尾部位置。
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): if None == A: return A len_A = len(A) m = 0 n = len_A - 1 while m <= n: if elem == A[m]: if elem != A : A[m], A = A , A[m] m += 1 n -= 1 else: n -= 1 else: m += 1 return n + 1
相关文章推荐
- 【Leetcode】【python】Container With Most Water
- 【LeetCode with Python】 Combination Sum II
- 【LeetCode with Python】 Insert Interval
- 【LeetCode with Python】 Search Insert Position
- **Leetcode_copy-list-with-random-pointer (c++ and python version)
- 【LeetCode with Python】 Populating Next Right Pointers in Each Node II
- 【LeetCode with Python】 Remove Duplicates from Sorted List II
- 【LeetCode with Python】 Longest Common Prefix
- 【LeetCode with Python】 Set Matrix Zeroes
- leetcode 日经贴,python code -Copy List with Random Pointer
- 【LeetCode with Python】 Wildcard Matching
- [leetcode]Substring with Concatenation of All Words @ Python
- 【LeetCode with Python】 Swap Nodes in Pairs
- 【LeetCode with Python】 Pow(x, n)
- 【LeetCode with Python】 Binary Tree Inorder Traversal
- 【LeetCode with Python】 Evaluate Reverse Polish Notation
- 【LeetCode with Python】 N-Queens
- 【LeetCode with Python】 Subsets
- 【LeetCode with Python】 Remove Nth Node From End of List
- 【LeetCode with Python】Remove Duplicates from Sorted Array II