본문 바로가기

알고리즘 & 자료구조569

이진 변환 반복하기 package programmers; import java.math.BigInteger; import java.util.Arrays; public class 이진변환반복하기 { public static int[] solution(String s) { StringBuilder sb; int zeroCount = 0; int binaryCount = 0; BigInteger bigInteger = new BigInteger(s); while ((bigInteger.compareTo(new BigInteger("1"))) > 0) { binaryCount++; sb = new StringBuilder(); for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == .. 2022. 12. 29.
올바른 괄호 package programmers; import java.util.Stack; public class 올바른괄호 { static boolean solution(String s) { Stack stack = new Stack(); if (s.charAt(0) == ')') { return false; } for (int i = 0; i 0) { return false; } return true; } public static void main(Stri.. 2022. 12. 28.
최솟값 만들기 package programmers; import java.util.Collections; import java.util.PriorityQueue; public class 최솟값만들기 { public static int solution(int[] A, int[] B) { int ans = 0; PriorityQueue a = new PriorityQueue(); PriorityQueue b = new PriorityQueue(Collections.reverseOrder()); for (int i = 0; i < A.length; i++) { a.add(A[i]); b.add(B[i]); } while (!a.isEmpty()) { ans += a.poll() * b.poll(); } return ans;.. 2022. 12. 27.
JadenCase 문자열 만들기 package programmers; import java.util.StringTokenizer; public class JadenCase문자열만들기 { // 모든 단어의 첫 문자는 대문자 // 그 외에 알파벳은 소문자 public static String solution(String s) { StringTokenizer st = new StringTokenizer(s, " ", true); StringBuilder sb = new StringBuilder(); while (st.hasMoreTokens()) { String str = st.nextToken(); if (str.startsWith(" ")) { sb.append(str); continue; } if (isStartWithNumber(st.. 2022. 12. 26.
최대값과 최솟값 package programmers; public class 최대값과최솟값 { public static String solution(String s) { String[] strings = s.split(" "); int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; for (String string : strings) { int number = Integer.parseInt(string); max = Math.max(max, number); min = Math.min(min, number); } return min + " " + max; } public static void main(String[] args) { System.out.println(solu.. 2022. 12. 25.
크기가 작은 부분 문자열 package programmers; public class 크기가작은부분문자열 { // 숫자로 이루어진 문자열 t, p // t 에서 p와 길이가 같은 부분 문자열 // 이 부분문자열이 나타내는 수가 p 가 나타내는 수보다 작거나 같은 것의 갯수 리턴 public static int solution(String t, String p) { int len = p.length(); long num = Long.parseLong(p); int answer = 0; for (int i = 0; i 2022. 12. 24.