Home [Leetcode] 1. Two Sum Array
Post
Cancel

[Leetcode] 1. Two Sum Array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        buff={}
        for i, n in enumerate(nums):
            d=target-n
            if n in buff:
                return [buff[n],i]
            else:
                buff[d]=i

Time complexity is O(n) with space complexity O(n).

This post is licensed under CC BY 4.0 by the author.

Torch adding custom loss functions

Git store credentials permanently