728x90
반응형

programmers.co.kr/learn/courses/30/lessons/12939

 

코딩테스트 연습 - 최댓값과 최솟값

문자열 s에는 공백으로 구분된 숫자들이 저장되어 있습니다. str에 나타나는 숫자 중 최소값과 최대값을 찾아 이를 (최소값) (최대값)형태의 문자열을 반환하는 함수, solution을 완성하세요. 예를

programmers.co.kr

 

#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdlib>
#include <stdlib.h>

using namespace std;

vector<string> split(string input, char delimiter) {
    vector<string> answer;
    stringstream ss(input);
    string temp;

    while (getline(ss, temp, delimiter)) {
        answer.push_back(temp);
    }
    return answer;
}

string solution(string s) {
    string answer = "";
    vector<string> nums = split(s, ' ');
    vector<int> num;
    int max = -9874;
    int min = 9874;

    for(int i= 0; i < nums.size(); i++){
        num.push_back(atoi(nums[i].c_str()));
        if(num[i] > max)
            max = num[i];
        if(num[i] < min)
            min = num[i];
    }
    answer = to_string(min) + " " + to_string(max);
    return answer;
}
728x90
반응형
728x90
반응형

프로그래머스 연습문제 올바른 괄호

올바른괄호 문제링크

조건 두면서 확인해가는 문제였습니다.

먼저 처음에 ')' 가 들어오면 False를 return 하도록 하고, 두 짝을 cnt 변수로 확인했습니다.

def solution(s):
    answer = True
    
    cnt = 0
    if s[0] == ')':
        return False
    for ch in s:
        if cnt == 0 and ch == ')':
            return False
        if ch == '(':
            cnt += 1
        if ch == ')':
            cnt -= 1
    if cnt != 0:
        return False

    return True

오늘은 이걸로 ㅎㅎ 

이만 :)

728x90
반응형

+ Recent posts