Make Unreal REAL.
article thumbnail
Level 1. [1차] 비밀지도

 

 

지도 1과 지도 2를 각각 계산하여 합칠 필요 없이 OR 연산자로 미리 합칠 수 있다.

 

& 1 연산으로 마지막 자리를 하나씩 가져와 벡터에 담고 transform 함수로 문자열로 변환했다.

 

벡터나 문자열에 값을 추가하는 도중에 메모리 재할당이 일어나지 않게 하기 위해 필요한 만큼의 크기를 미리 할당했다.

 

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<bool> toBinary(int n, int value)
{
    vector<bool> v(n);

    for (auto rit = v.rbegin(); rit != v.rend(); ++rit)
    {
        *rit = value & 1;
        value >>= 1;
    }

    return v;
}

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

    for (int i = 0; i < n; ++i)
    {
        string s(n, ' ');
        vector<bool> binary = toBinary(n, (arr1[i] | arr2[i]));

        transform(binary.begin(), binary.end(), s.begin(), [](bool b) { return b ? '#' : ' '; });

        answer[i] = s;
    }

    return answer;
}

'자료구조 & 알고리즘 > 프로그래머스' 카테고리의 다른 글

Level 1. 폰켓몬  (0) 2023.02.16
Level 1. 푸드 파이트 대회  (0) 2023.02.15
Level 1. 콜라 문제  (0) 2023.02.13
Level 1. 크기가 작은 부분 문자열  (0) 2023.02.12
Level 1. 삼총사  (0) 2023.02.11
profile

Make Unreal REAL.

@diesuki4

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그