My Python code O(N)


  • 0
    L

    Count the occurrence of each number

            length = len(nums)
            if length<=1:
                return length
                
            i = 0
            cnt = 0
            for j in range(1,length):
                if nums[j] == nums[i]:
                    cnt += 1
                else:
                    i += 1
                    nums[i] = nums[j]
                    cnt = 0
                
                if cnt == 1:
                    i += 1
                    nums[i] = nums[j]
                
            return i+1
    

Log in to reply
 

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