반응형
오늘은 JAVA로 풀었던 Two Sum을 Python으로 풀어보기 연습. 은 사실 어제 문제였지만.. 그리고 '다시 푼다'기 보단 자바 코드를 파이썬 코드로 옮기는 것에 가까운 것 같다. 이것도 생각보다 노력이 필요한 일이긴 하다...
class Solution:
#used Dictionary
def twoSum(self, nums: List[int], target: int) -> List[int]:
ansMap = {}
for i in range(len(nums)):
if target-nums[i] in ansMap:
return [ansMap.get(target-nums[i]), i]
else:
ansMap[nums[i]] = i
i+=1
#used double for clause
def twoSum2(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if nums[i]+nums[j] == target:
return [i, j]
i+=1
아래는 자바로 풀었던 포스팅 링크.
[LeetCode] Two Sum
오늘의 문제. 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 exactly one solution,..
jazzodevlab.tistory.com
728x90
반응형
'TIL > Coding Test' 카테고리의 다른 글
[LeetCode] Convert Binary Number in a Linked List to Integer (0) | 2021.04.05 |
---|---|
[LeetCode] assign cookies (Python) (0) | 2021.04.02 |
[LeetCode] Lemonade Change (Python) (0) | 2021.03.29 |
[백준] 베스트셀러 Best Seller (0) | 2021.03.29 |
[프로그래머스] 오픈채팅방 (4) | 2021.03.24 |