4.[easy] remove elements

来源:互联网 发布:linux怎么yum安装gcc 编辑:程序博客网 时间:2024/06/06 02:42

Given an array and a value, remove all instances of that value in place and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

就地移除数组中指定值的所有元素

对python来说这个问题就是a piece of cake

def remove_elements(num_list, value):    while True:        try:            num_list.remove(value)        except ValueError:            return num_list