-
[leetcode] 1. Two SumAlgorithm/1일 1코테 2021. 5. 4. 19:08반응형
leetcode.com/problems/two-sum/submissions/
내 풀이
for i in range(len(nums)): t = target - nums[i] if t in nums and nums.index(t) != i: return nums.index(t), i
해쉬를 이용한 풀이
class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: nums_map = {} for i, num in enumerate(nums): nums_map[num] = i for i, num in enumerate(nums): if target - num in nums_map and i != nums_map[target-num]: return nums.index(num), nums_map[target-num]
반응형'Algorithm > 1일 1코테' 카테고리의 다른 글
[프로그래머스] 2단계 신규 아이디 추천 (1) 2021.06.23 [프로그래머스] H-index (0) 2021.06.11 [leetcode] 819. Most Common Word (0) 2021.04.28 [leetcode] 937. Reorder Log Files (0) 2021.04.28 [leetcode] 344. Reverse String (0) 2021.04.14