isdigit 사용해서 반복문으로 풀었음.

#include <string>

using namespace std;

bool solution(string s) {
   
	if (s.size() != 4 && s.size() != 6)
		return false;

	for (int i = 0; i < s.size(); i++)
		if (!isdigit(s[i]))
			return false;

	return true;
}

sort함수의 comp 함수만 조절해 내림차순으로 sorting했다.

#include <string>
#include <algorithm>

using namespace std;

bool comp(char a, char b)
{
    return a > b;
}

string solution(string s) {

    sort(s.begin(), s.end(), comp);

    return s;
}

transform이 아니라 반복문으로 풀었음.

#include <string>

using namespace std;

bool solution(string s)
{
    int p_num = 0, y_num = 0;

    for (int i = 0; i < s.size(); i++)
    {
        if (s[i] == 'P' || s[i] == 'p') 
            p_num++;
        else if (s[i] == 'Y' || s[i] == 'y') 
            y_num++;
    }

    return p_num == y_num;
}

sort의 compare 인자만 바꾸면 쉽다.

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

using namespace std;

int N;

bool comp(string a, string b)
{
    if (a[N] == b[N])
        return a < b;
    else
        return a[N] < b[N];
}

vector<string> solution(vector<string> strings, int n) {
    
    N = n;

    sort(strings.begin(), strings.end(), comp);

    return strings;
}

그냥 반복문으로 풀었던 문제다.

#include <vector>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> arr, int divisor) {
    vector<int> answer;

    for (int i = 0; i < arr.size(); i++)
        if (arr[i] % divisor == 0)
            answer.push_back(arr[i]);

    if (answer.size() == 0)
        answer.push_back(-1);
    else
        sort(answer.begin(), answer.end());

    return answer;
}

+ Recent posts