LeetCode/릿코드-Two Sum-python

문제

풀이

  • given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
  • you may assume that each input would have exatly one solution, and you may not use the same element twice.
  • you can return the answer in any order.
  • follow-up: can you come up with an algorithm that is less than o(n^2) time complexity?

코드

# leetcode, easy : two sum, python3
# array, hash table
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        for x in range(len(nums)):
            for y in range(x+1, len(nums)):
                if nums[x] + nums[y] == target:
                    return [x, y]

결과



출처 && 깃허브

LeetCode
github

좋은 웹페이지 즐겨찾기