Saad-data-zz/Internview.question_optimal_list
Task 要修改一个列表lista = [1,2,4,4,10,6,7,5,20],用以下代码把其中偶数删除,但最终得到的结果是有部分偶数没有删除,为什么没有删除干净? for i in lista: if i % 2 == 0: lista.remove(i) 请用python写一个方法,以最小的代价来把所有偶数都删除,并说明为什么这样写可以删除所有偶数? 如果方法的时间复杂度为O(n),空间复杂度为O(1),要怎么实现? English Translation : To modify a list lista = [1,2,4,4,10,6,7,5,20], use the following code to delete the even numbers, but the final result is that some even numbers are not deleted, why are they not deleted cleanly ? for i in lista: if i % 2 == 0: lista.remove(i) Please write a method in python to delete all even numbers with the least cost, and explain why this writing can delete all even numbers? If the time complexity of the method is O(n) and the space complexity is O(1), how to implement it?
Jupyter Notebook