-
-
Notifications
You must be signed in to change notification settings - Fork 245
[rivkode] WEEK 14 Solutions #1955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| 입력받은 n에 대해 toBinaryString() 을 사용하여 binaryString 값을 알아낸뒤 charAt()으로 각 인덱스별 접근하여 1의 개수를 찾아내는 방식이다. | ||
| */ | ||
|
|
||
| class Solution { | ||
| public int[] countBits(int n) { | ||
| int[] answer = new int[n + 1]; | ||
| for (int i=0; i<n+1; i++) { | ||
| String bit = Integer.toBinaryString(i); | ||
| System.out.println(bit); | ||
| int cnt = 0; | ||
| for (int j=0; j < bit.length(); j++) { | ||
| char c = bit.charAt(j); | ||
| int num = c-'0'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 코드가 어떤 역할을 하는 건지 궁금해요. 무작정 2로 나눠서 몫과 나머지를 통해 답을 도출한다는 생각만 했더니... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 안녕하세요 ㅎㅎ 아스키 코드를 사용하였습니다. 여기서 |
||
| if (num == 1) { | ||
| cnt += 1; | ||
| } | ||
| } | ||
|
|
||
| answer[i] = cnt; | ||
| } | ||
|
|
||
| return answer; | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
자바에 toBinaryString 같은 함수가 존재하는지 몰랐네요!