Python while using runner O(n) solution


  • 0
    N
    class Solution(object):
        def removeDuplicates(self, nums):
            if not nums:
                return 0
            if len(nums) == 1:
                return 1
    
            i, runner = 0, 1
            while runner < len(nums):
                if nums[runner] != nums[i]:
                    i += 1
                    nums[i] = nums[runner]
                runner += 1
    
            return i + 1
    

  • 0

    @nirnaor Why the if len(nums) == 1: return 1? Doesn't that just slow it down for almost every input, and mislead people into thinking that the rest of your code would fail for inputs of length 1?


  • 0
    N

    You are correct. I missed that.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.