Make Unreal REAL.
article thumbnail
Level 1. 크기가 작은 부분 문자열

Level 1. 크기가 작은 부분 문자열 크게 더 고민해 볼 것은 없는 문제였다. #include #include using namespace std; int solution(string t, string p) { int answer = 0; double numP = stod(p); size_t lenT = t.length(), lenP = p.length(); size_t last = lenT - lenP; for (int i = 0; i

article thumbnail
Level 1. 삼총사

Level 1. 삼총사 next_permutation() 함수는 현재 순열의 다음 순열을 만든다. 1, 2, 3, 4의 경우 1, 2, 4, 3 1, 3, 2, 4 1, 3, 4, 2 1, 4, 2, 3 ... 비트마스크에 순열을 적용하면 조합을 만들 수 있다. 4개 중 2개를 뽑을 경우 0, 0, 1, 1 0, 1, 0, 1 0, 1, 1, 0 1, 0, 0, 1 ... #include #include #include using namespace std; int solution(vector number) { int answer = 0; vector comb(number.size() - 3, 0); comb.insert(comb.end(), {1, 1, 1}); do { int sum = 0; auto ..

article thumbnail
Level 1. 같은 숫자는 싫어

Level 1. 같은 숫자는 싫어 unique() 함수는 양옆의 원소를 비교하여 중복 원소들을 끝으로 몰아 놓는다. #include #include #include using namespace std; vector solution(vector arr) { arr.erase(unique(arr.begin(), arr.end()), arr.end()); return arr; } 스택을 이용해 최초 원소를 삽입하고 중복 원소는 삽입하지 않으며 새로운 원소가 나오면 pop() 후 push()한다. 문제 유형에 라고 나와있어서 스택을 사용해봤는데 사실 사용하지 않아도 된다. #include #include #include using namespace std; vector solution(vector arr) { ..

article thumbnail
Level 1. 문자열 내 p와 y의 개수

Level 1. 문자열 내 p와 y의 개수 count_if() 함수를 활용하면 쉽게 해결할 수 있다. #include #include #include using namespace std; bool solution(string s) { return count_if(s.begin(), s.end(), [](const char c) { return tolower(c) == 'p'; }) == count_if(s.begin(), s.end(), [](const char c) { return tolower(c) == 'y'; }); } 직접 구현하면 2번을 따로 순회할 필요 없이 1번만 순회해도 된다. #include #include using namespace std; bool solution(string s)..

article thumbnail
Level 0. 유한소수 판별하기

Level 0. 유한소수 판별하기 유한 소수인 분수는 다음을 만족한다. 최소공배수를 구하여 약분하기보다는 이 풀이가 좀 더 간단하고 효율적이라고 생각한다. 분모를 먼저 2와 5로 나누어 떨어지지 않을 때까지 나눈다. 이때 분자가 분모로 나누어 떨어지면 유한 소수로 나타낼 수 있는 분수이다. #include using namespace std; int solution(int a, int b) { while ((b & 1) == 0) b >>= 1; while (b % 5 == 0) b /= 5; return (a % b) ? 2 : 1; }

article thumbnail
Level 0. 소인수분해

Level 0. 소인수분해 소인수분해는 다음과 같은 과정을 통해 수행할 수 있다. n = 120을 예로 들 때 i는 2부터 시작한다. n이 1이 아닌 동안 반복한다. 1씩 증가시키며 나누어 떨어지는 수 i를 찾는다. i를 소인수 리스트에 추가한다. n이 더 이상 나누어 떨어지지 않을 때까지 i로 나눈다. 2로 돌아가 과정을 반복한다. 아래는 재귀를 이용하여 부문제로 나누어 해결한 풀이이다. 예들 들면 solution(120)은 2와 solution(60)의 결과를 합친 것이다. solution(60)은 2와 solution(30)의 결과를 합친 것이다. solution(30)은 2와 solution(15)의 결과를 합친 것이다. solution(15)는 3과 solution(5)의 결과를 합친 것이다. ..

검색 태그