반응형 Java2 K번째수_프로그래머스 정수형 배열 array 값들이 주어지고, 그 array에서 잘라낼 첫 번째 인덱스와 마지막 인덱스가 이차형 정수 배열 commands의 commands[i][0], commands[i][1]로써 주어진다. 잘라낸 수의 배열을 만들어 정렬한 후, 뽑아내고 싶은 숫자의 인덱스가 commands[i][2]로 주어진다. import java.util.*; class Solution { public int[] solution(int[] array, int[][] commands) { int[] answer = new int[commands.length]; int idxForAnswer = 0; for (int i = 0; i < commands.length; i++) { int[] tmp = new int[com.. 2024. 2. 29. bitCount 메서드 사용 (다음 큰 숫자_프로그래머스) 10 진수 숫자가 주어지고, 그 수를 1씩 증가시킨다. 처음 주어진 숫자를 2 진수로 바꿔주고 그 수를 구성하는 1의 개수를 구한다. 1씩 증가하는 수를 2 진수화 시킨다. 이 때, 2 진수에 구성되는 1의 개수가 같은 다음 수를 구하는 문제. Integer 클래스의 bitCount() 메서드를 사용하면 간단하게 1인 비트 수를 셀 수 있다. class Solution { public int solution(int n) { int num = Integer.bitCount(n); int answer = n; while (true) { answer = answer + 1; int cnt = Integer.bitCount(answer); if (num == cnt) break; } return answer; .. 2024. 2. 20. 이전 1 다음 반응형