본문 바로가기
Algorithm🐰/리트코드

[리트코드] 1. Two Sum (두수의 합)

by Jouureee 2021. 7. 12.

문제 :

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

댓글