๋ฌธ์ ๋งํฌ : https://leetcode.com/problems/two-sum/description/
ย
๐ฅ๏ธย ์์ํ๋ฉฐ
๋ธ๋ฃจํธ ํฌ์ค ๋ฐฉ๋ฒ์ผ๋ก ์ ๊ทผํ ์ ์๋ค.
ย
์ด์ค for๋ฌธ ์ผ๋ก ์์ฐจ์ ์ผ๋ก ๋ชจ๋ ๋ฐฐ์ด์ ํ์ํ๋ฉฐ ๊ฒฐ๊ด๊ฐ์ ์ฐพ์ผ๋ฉด ๋๋ค. ์กฐ๊ฑด์ You may assume that each input would haveย exactlyย one solution, ๋ผ๊ณ ๋ช
์ํ์ผ๋ฏ๋ก ์ฌ์ด ๋ฌธ์ ๋ผ ํ ์ ์๋ค.ย
โ๏ธย Python
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]
โ๏ธย C++
class Solution { public: vector<int> twoSum(vector<int> &nums, int target) { for (int i = 0; i < nums.size(); ++i) { for (int j = i + 1; j < nums.size(); ++j) { if (nums[i] + nums[j] == target) { return {i, j}; // ๋ ์ธ๋ฑ์ค๋ฅผ ๋ฒกํฐ๋ก ๋ฐํ } } } return {}; } };
ย
ย
๐ย ์๊ฐ
์ฌ์ด ๋ฌธ์ ๋ค.
ย
๐ย ๋ถ๋ก
๐ย ์ฐธ๊ณ ๋ฌธํ
ย
![[Leetcode] 1. Two Sum](https://reo91004.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb4dac9d0-810c-47c4-800c-0ca10b8d0529%2Fde29f40a-05a3-4aec-b112-7e56072966a0%2FLeetCode_Logo_Black.png?table=block&id=9100f40c-024a-4f89-948d-65185995397c&cache=v2)
![[Leetcode] 1. Two Sum](https://reo91004.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2Fb4dac9d0-810c-47c4-800c-0ca10b8d0529%2F45a3427e-47fb-412a-a090-3bf6d1afdb04%2Fleetcode.png?table=block&id=9100f40c-024a-4f89-948d-65185995397c&cache=v2)

๋๊ธ