티스토리 뷰

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

string solution(string s) {
    string answer = "";
    vector<int> str;
    string temp = "";
    int i = 0;
    
    while(s[i] != '\0'){
        if(s[i] == ' ') {
            str.push_back(stoi(temp));
            temp = "";
        }
        else temp = temp + s[i];
        i += 1;
    }
    if(temp != "") str.push_back(stoi(temp));
    answer = to_string(*min_element(str.begin(), str.end())) + " " + to_string(*max_element(str.begin(), str.end()));
    return answer;
}

처음에는 어 뭐양., 이게 왜 레벨 2라는거야 ~~ 이랬는데

생각보다 공백을 기준으로 음수와 양수를 숫자로 바꾸는게 쨰끔 어려웠다.

그래서 공백이 나올 때 까지 temp 문자열에 넣고 공백이 나오면 인티져형으로 바꿔서 str 벡터에 넣었다.

마지막에는 공백이 안나오고 s 문자열이 끝나므로 temp에 남아있는 문자열을 마저 넣어준다.

최댓값과 최솟값은 algorithm 헤더에서 max_element, min_element 함수를 사용했다.

댓글