구간합 문제다.

정수의 구간 합 문제이므로 (a + b) * (b - a + 1) / 2 하면 된다. (a < b)

#include <cmath>
#include <vector>

using namespace std;

long long solution(int a, int b) {
    return (long long)(a + b) * (abs(a - b) + 1) / 2;
}

연속된 수 제거이므로 sorting하지않고 unique, erase 사용해서 풀면 된다.

#include <vector>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> arr)
{
    arr.erase(unique(arr.begin(), arr.end()), arr.end());

    return arr;
}

시키는대로 하면 되는 문제였다.

다만 10처리 해줄때만 조심하면 되었다.

#include <string>

using namespace std;

int solution(string dartResult) {
    int answer = 0;

    int prev = 0;
    for (int i = 0; i < dartResult.size(); i += 2)
    {
        int score = dartResult[i] - '0';
        
        if (dartResult[i + 1] == '0')
        {
            score = 10;
            i++;
        }
        if (dartResult[i + 1] == 'D')
            score *= score;
        if (dartResult[i + 1] == 'T')
            score *= score * score;

        if (dartResult[i + 2] == '#')
        {
            score *= -1;
            i++;
        }
        else if (dartResult[i + 2] == '*')
        {
            score *= 2;
            answer += prev;
            i++;
        }

        answer += score;
        prev = score;
    }
    return answer;
}

그냥 구현하면 됨

몇글자 안되서 substr 안썻음

#include <string>
#include <vector>

using namespace std;

string solution(string s) {
    string answer = "";

    if (s.size() % 2 == 0)
    {
        answer += s[s.size() / 2 - 1];
        answer += s[s.size() / 2];
    }
    else
        answer += s[s.size() / 2];

    return answer;
}

비트연산으로 푸는 문제.

#include <string>
#include <vector>

using namespace std;

vector<string> solution(int n, vector<int> arr1, vector<int> arr2) {
    vector<string> answer;

    for (int i = 0; i < n; i++)
    {
        string in = "";

        int temp = arr1[i] | arr2[i];
        int mod = 1 << n - 1;

        for (int j = 1; j <= n; j++)
        {
            if (temp & mod)
                in += '#';
            else
                in += ' ';
            mod = mod >> 1;
        }
        answer.push_back(in);
    }
    return answer;
}

+ Recent posts