Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions counting-bits/rivkode.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
/*
1. 문제 이해
n 을 입력받았을때 n+1 배열의 길이에서 각 인덱스 i 위치에 i 를 이진수로 전환했을때의 1의 개수를 입력한 배열을 반환

2. 구현
for 문을 돌면서 이진수를 변환하여 1의 개수를 카운팅한 값을 넣어준다
bitCount() 메서드를 사용할 수 있다.

*/

import java.util.*;

class Solution {
public int[] countBits(int n) {
int[] arr = new int[n+1];

for (int i=0; i<n+1; i++) {
int count = Integer.bitCount(i);
Copy link
Member

@DaleSeo DaleSeo Feb 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코딩 테스트에서 요런 언어에 내장된 함수를 사용하시면 득보다 해가 되는 경우가 있어서 참고로 말씀드립니다. 특히 이 프로그래밍 언어를 모르는 면접관은 이 함수가 내부적으로 어떤 구현이 되었는지 물어볼 확률이 높은데, 이 때 깊이있게 설명하지 못하면 제대로 알지 못하고 쓴다는 인상을 줄 수 있거든요. 출제 의도가 알고리즘과 자료구조에 능력을 평가하기 위함이니 다이나믹 프로그래밍과 같은 방식으로 직접 구현하시는 게 코딩 테스트 한정으로는 더 안전한 선택이 될 거라고 생각합니다.

arr[i] = count;
}

return arr;
}
}

/*
입력받은 n에 대해 toBinaryString() 을 사용하여 binaryString 값을 알아낸뒤 charAt()으로 각 인덱스별 접근하여 1의 개수를 찾아내는 방식이다.
*/
Expand Down