Only removing is easy, while the problem requires no extra space, so we must operate on given list.
Because sequence is not required, we can create a pointer pointing at inserting position, when see different numbers, we swap it from back to the inserting position.
Solution:
# T:O(n) S:O(1) class Solution: # @param a list of integers # @return an integer def removeDuplicates(self, A): if len(A) == 0: return 0 j = 0 for i in range(len(A)): if A[i] != A[j]: A[i], A[j+1] = A[j+1], A[i] j = j + 1 return j+1Run Time: 92 ms
No comments:
Post a Comment