๋ฌธ์ :
programmers.co.kr/learn/courses/30/lessons/68644
๋ถ์ :
input vector : numbers
output vector : answer
ex>
input | output |
[2,1,3,4,1] | [2,3,4,5,6,7] |
[5,0,2,7] | [2,5,7,9,12] |
๊ฐ ์ฃผ์ด์ง ๋
์ฒซ๋ฒ์งธ for ๋ฌธ : inter ๋ ๋ฐ๋ณตํ ๊ฐ๊ฒฉ์ด๋ค. 1 ~ size-1 ๋ฒ์๋ฅผ ๊ฐ์ง๋ค.
๋๋ฒ์งธ for ๋ฌธ : j๋ ๋ ์ธ๋ฑ์ค, j์ ๋ฒ์๋ ๊ฐ๊ฒฉ ๋ง์ง๋ง ์์๊ฐ ์ฐธ์กฐ๋๋ ๊ณณ ๊น์ง ๋ฐ๋ผ์ 0 ~ size - inter ๋ค.
find ํจ์ ( vector.begin(), vector.end(), ์ฐพ์ ๊ฐ)
์์์ ์ธ๋ฑ์ค ๋ฐํํ๋๋ฐ answer์ ์์ ๊ฒฝ์ฐ answer.end()์ ๊ฐ์ ๊ฐ์ ๋ฐํํ๋ค.
๋ง์ง๋ง์ ์ค๋ฆ์ฐจ์ ์ ๋ ฌ sort
c++ ์ฝ๋ :
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
vector<int> solution(vector<int> numbers) {
vector<int> answer;
for(int inter = 1; inter < (int)numbers.size(); inter++){
for(int j = 0; j < (int)(numbers.size()) - inter; j++){
int temp = numbers[j] + numbers[j+inter];
if(find(answer.begin(), answer.end(), temp) == answer.end()){
answer.push_back(temp);
}
}
}
sort(answer.begin(), answer.end());
return answer;
}
๋ ๋ฒจ 1๋ ๋ฒ๊ฒ๋ค ์กฐ๊ธ์ฉ ์ ์กฐ์ ธ๋ณด์ ํํ
'Algorithm๐ฐ > ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] Summer/Winder coding ๋ฐฉ๋ฌธ ๊ธธ์ด level2 (0) | 2021.04.30 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ์์ ๊ฒ์ 2021 ์นด์นด์ค ๋ธ๋ผ์ธ๋ ํ ์คํธ level2 (0) | 2021.04.28 |
[ํ๋ก๊ทธ๋๋จธ์ค] ํฌ๋ ์ธ ์ธํ ๋ฝ๊ธฐ 2019 ์นด์นด์ค ๊ฐ๋ฐ์ ๊ฒจ์ธ ์ธํด์ฝ (0) | 2021.02.24 |
[ํ๋ก๊ทธ๋๋จธ์ค] 2016๋ basic (0) | 2021.02.24 |
[ํ๋ก๊ทธ๋๋จธ์ค] ํ์ค๋ ์ basic (0) | 2021.02.24 |
๋๊ธ