File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int [] countBits (int n ) {
3+ // 0 ~ n 까지 2진수를 구하고, 그 수의 모든 자릿값의 합을 구해야함
4+
5+ int [] result = new int [n + 1 ];
6+
7+ for (int i = 0 ; i <= n ; i ++) {
8+ String binaryString = Integer .toBinaryString (i );
9+ int sum = 0 ;
10+ for (int j = 0 ; j < binaryString .length (); j ++) {
11+ sum += binaryString .charAt (j ) - '0' ;
12+ }
13+ result [i ] = sum ;
14+ }
15+
16+ return result ;
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int reverseBits (int n ) {
3+ String origin = Integer .toBinaryString (n );
4+ origin = String .format ("%32s" , origin ).replace (' ' , '0' );
5+ String reversed = "" ;
6+ for (int i = origin .length () - 1 ; i >= 0 ; i --) {
7+ reversed += origin .charAt (i );
8+ }
9+ // System.out.println(origin);
10+ // System.out.println(reversed);
11+ int result = Integer .parseInt (reversed , 2 );
12+ // System.out.println(result);
13+ return result ;
14+ }
15+ }
You can’t perform that action at this time.
0 commit comments