Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

1. Two Sum

class Solution:
    def twoSum(self, A: List[int], target: int) -> List[int]:
        seen = {}

        for i, v in enumerate(A):
            need = target - v
            if need in seen:
                return [seen[need], i]
            seen[v] = i

        return []
MetricComplexityReason
Time ComplexityO(n)Single pass through the list
Space ComplexityO(n)Dictionary to store seen elements