Algorithm๐ฐ/๋ฆฌํธ์ฝ๋
[๋ฆฌํธ์ฝ๋] 1. Two Sum (๋์์ ํฉ)
Jouureee
2021. 7. 12. 03:07
๋ฌธ์ :
https://leetcode.com/problems/two-sum/
Two Sum - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
๋์ด๋ : easy
ํ์ด ๋ฐฉ๋ฒ : ๋ถ๋ฅดํธํฌ์ค O(n^2)
c++ ์ฝ๋ :
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> answer;
for(int i = 0; i < nums.size(); i++){
for(int j = i + 1; j < nums.size(); j++){
if(nums[i] + nums[j] == target){
answer.push_back(i);
answer.push_back(j);
return answer;
}
}
}
return answer;
}
};