https://leetcode.com/problems/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, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Output: [0,1] Output: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Output: [1,2]
Example 3:
Output: [0,1]```
풀이)
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
for j in range(len(nums)):
if i == j:
continue
if nums[i] + nums[j] == target:
num_index = [i, j]
return num_index
target
과 같으면 된다.continue
를 통해 그 다음 for문 iter로 가게 한다.