//GitHub https://github.com/vellimole0621
(문제 - 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.
=> int로 이루어진 배열 nums 와 int target이 주어질 때, nums의 두 개의 원소 값 합이 target이 되는 두 원소 인덱스를 출력하라.
풀이 : 정답 넣을 배열 생성 > 이중 반복문으로 각각 두 원소를 더해가면서 target과 비교 > 같을 시, 정답 배열에 넣고 출력
class Solution:
def twoSum(self, nums, target):
# 정답 넣을 배열 생성
answer = []
# 이중 반복문으로 해당 인덱스값 찾기 시도
for i in range(len(nums) - 1):
for j in range(i + 1, len(nums)):
# 두 수의 합이 target과 같을 때, 정답 배열에 넣고 출력
if (nums[i] + nums[j]) == target:
answer.append(i)
answer.append(j)
return answer
break
'프로그래밍 > Python' 카테고리의 다른 글
[파이썬 Python/알고리즘] LeetCode - Unique Paths (0) | 2023.07.16 |
---|---|
[파이썬 Python/알고리즘] LeetCode - Single Number II (0) | 2023.07.04 |
[파이썬 Python/알고리즘] LeetCode - Buddy Strings (0) | 2023.07.03 |
[파이썬 Python/알고리즘] LeetCode - Palindrome Number (0) | 2023.07.02 |
[Python] 블랙잭 게임 (0) | 2023.02.26 |