자바 8

프로그래머스 - 다음 큰 숫자

Integer.toBinaryString을 사용해 1의 개수를 구해놓고 n++을 하면서 1의 개수가 같은 수를 구하는 방법을 사용했습니다! class Solution { public int solution(int n) { int answer = 0, cnt = 0, binN = n + 1, binCnt = 0; String strN[] = Integer.toBinaryString(n).split(""); for(String str : strN) if(str.equals("1")) cnt++; while(true){ binCnt = 0; String strBin[] = Integer.toBinaryString(binN).split(""); for(String str : strBin) if(str.equal..

코딩테스트 2023.12.31

프로그래머스 - 소인수분해

다들 소인수분해 어떻게 하시나요 저는 이렇게 합니다!! 여기서 아이디어를 얻었는데요! 2부터 시작해 나누어 떨어지지 않을 때까지 나누다보면 다음 소인수로 넘어갑니다 for문을 사용해 2부터 n까지 돌려주고, 그 안에서 while문으로 나누어 떨어지는 동안만 반복하게 하는 것입니다! 바로 코드 진행시켜! import java.util.*; class Solution { public int[] solution(int n) { ArrayList list = new ArrayList(); for(int i = 2; ii).toArray(); } } while문은 참인 동안 반복문을 수행합니다! 만약 중복인 값이 없다면 list에 넣어줍니다! 계속 n을 i로 나누어주다 보면 제가 구하는 방식처럼 어느 순간 나누어..

코딩테스트 2023.12.30

프로그래머스 - 배열의 길이를 2의 거듭제곱으로 만들기

처음 생각한 방법은 ArrayList를 사용해 arr의 값을 옮겨준 다음 list의 길이가 2의 거듭제곱보다 작을 때의 거듭제곱을 구해 list에 거듭제곱이 될 때까지 0을 넣는 방법이었습니다! 처음에 ArrayList에 arr의 값을 옮겨주고~ ArrayList list = new ArrayList(); for(int n : arr) list.add(n); Math.pow()를 사용해 2의 거듭제곱과 list의 길이를 비교했습니다! int res = 0; for(int i = 0; i

코딩테스트 2023.12.21

프로그래머스 - 세 개의 구분자

저는 split을 사용해서 문자열을 나누어 준 다음 ArrayList를 사용해 문자열의 길이를 비교해주는 방법을 생각했습니다! import java.util.*; class Solution { public String[] solution(String myStr) { String[] s = myStr.split("[abc]"); ArrayList list = new ArrayList(); for(String str : s){ if(str.length() > 0) list.add(str); } if(list.size() == 0) list.add("EMPTY"); String[] answer = new String[list.size()]; int n = 0; for(String str : list) answ..

코딩테스트 2023.12.20