[Leetcode] 1. Two Sum
[Leetcode] 1. Two Sum

[Leetcode] 1. Two Sum

์–ธ์–ด
Python
C
๋‚œ์ด๋„
๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ
๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜ ์œ ํ˜•
์ž‘์„ฑ์ž
๋ฐ•์šฉ์„ฑ๋ฐ•์šฉ์„ฑ
์ƒ์„ฑ ์ผ์‹œ
2024๋…„ 06์›” 02์ผ
๋ฌธ์ œ ๋งํฌ : 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 {}; } };
ย 
ย 

๐Ÿ“Œย ์†Œ๊ฐ

์‰ฌ์šด ๋ฌธ์ œ๋‹ค.
ย 

๐Ÿ”ย ๋ถ€๋ก

๐Ÿ”ย ์ฐธ๊ณ ๋ฌธํ—Œ


ย 

๋Œ“๊ธ€

guest