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;
    }
    
};