Publish:

태그: ,

카테고리:

https://school.programmers.co.kr/learn/courses/30/lessons/12939
난이도 ⭐⭐

문제

12939


나의 풀이

먼저 string에 있는 정수를 가져와야 한다.
그래서 string을 ‘ ‘구분자로 나눠서 int로 변환한 뒤 vector<int> numbers에 저장한다.
이후 numberssort하여(오름차순) 첫 값(최솟값)과 끝 값(최댓값)을 string으로 형변환하여 리턴.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>

using namespace std;

vector<int> split(string str, char Delimiter) {
    istringstream iss(str);             
    string buffer;                      

    vector<int> result;

    while (getline(iss, buffer, Delimiter))
    {

        result.push_back(stoi(buffer));
    }

    return result;
}

string solution(string s)
{
    vector<int> numbers = split(s, ' ');

    string answer = "";
    string number = "";

    sort(numbers.begin(), numbers.end());

    answer = to_string(numbers.front()) + " " + to_string(numbers.back());

    return answer;
}

이슈 및 공부한 것을 기록해두는 개인 블로그 입니다. 댓글, 피드백 환영합니다 🙂

Update:

댓글남기기