[LeetCode] Two Sum

 

1. ๋ฌธ์ œ : Link

๋ฐฐ์—ด ์•ˆ ๋‘ ์ˆ˜๋ฅผ ํ•ฉํ–ˆ์„๋•Œ target์ด ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ, ๊ทธ๋•Œ์˜ ์ธ๋ฑ์Šค๋ฅผ return

 

2. ํ’€์ด

๋ชจ๋“  ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋Š” brute force ๋ฅผ ์ ์šฉํ•ด์„œ target๊ฐ’์ด ๋งŒ์กฑ๋˜๋Š” ์ธ๋ฑ์Šค๊ฐ€ ๋‚˜์˜ค๋ฉด ๋ฐ”๋กœ ๋‹ต์œผ๋กœ return

 

3. ์ฝ”๋“œ

class Solution:
    def twoSum(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]

'๐Ÿ’ป Coding Problems Solving > Array | String | Loop' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[LeetCode] Contains Duplicate  (0) 2022.07.09
[LeetCode] Best Time to Buy and Sell  (0) 2022.07.03
[BOJ 1789] ์ˆ˜๋“ค์˜ ํ•ฉ  (0) 2022.06.26
[BOJ 1978] ์†Œ์ˆ˜์ฐพ๊ธฐ  (0) 2022.06.22
[BOJ 2693] N๋ฒˆ์งธ ํฐ ์ˆ˜  (0) 2022.06.22
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ