https://leetcode-cn.com/problems/remove-element/submissions/
# -*- coding:utf-8 -*- class Solution(object): def removeElement(self, nums, val): """ :type nums: List[int] :type val: int :rtype: int """ for i in range(len(nums) - 1, -1, -1): if nums[i] == val: nums.pop(i) return len(nums) if __name__ == '__main__': nums = [3, 2, 2, 3] print(Solution().removeElement(nums, 3))
思路:做过前面0026这道题就很好做了,倒序的理由也一样,只不过判断条件稍微改变。
0 条评论