목록Java (133)
코딩응급실
import java.util.*; public class Solution { public int[] solution(String msg) { // 결과를 저장할 ArrayList 선언 (동적 크기 조정이 필요하기 때문) ArrayList resultList = new ArrayList(); // 사전 초기화: 각 알파벳에 대응하는 색인 번호 저장 Map dictionary = new HashMap(); for (int i = 0; i < 26; i++) { char ch = (char) ('A' + i); dictionary.put(String.valueOf(ch), i + 1); } for (int i = 0; i < msg.length();) { // 사전에서 현재 입력과 일치하는 가장 긴 문자열 ..
import java.util.*; class Solution { public int solution(int n, int k) { // n을 k진수로 변환 String kBaseNum = Integer.toString(n, k); // 변환된 수에서 조건에 맞는 소수 찾기 int count = 0; String[] candidates = kBaseNum.split("0"); // 0을 기준으로 나눔 for (String candidate : candidates) { if (!candidate.isEmpty() && isPrime(Long.parseLong(candidate))) { count++; } } return count; } // 소수 판별 함수 private boolean isPrime(long..
지나간 길은 세지 않고 처음 가는 길만 세야 합니다. 좌표평면의 경계를 넘어가는 명령어는 무시하고, 방문한 길들을 visitedPaths라는 HashSet에 저장하여 중복된 길의 기록을 방지합니다.( 세트(Set)는 중복을 허용하지 않으므로, 이미 걸어본 길은 중복 추가되지 않습니다. 따라서, visitedPaths에는 실제로 처음 걸어본 길만이 저장됩니다. ) 각 이동 경로는 출발점과 도착점을 문자열로 변환하여 저장합니다. 이 때, 양방향을 모두 저장하는 이유는 캐릭터가 같은 길을 반대 방향으로 이동할 수 있기 때문입니다. 마지막으로, 처음 걸어본 길의 길이는 visitedPaths의 크기의 절반입니다(각 길이 양방향으로 저장되었기 때문에). import java.util.*; class Solutio..
import java.util.*; class Solution { public int[] solution(int[] numbers) { int[] result = new int[numbers.length]; // 결과를 저장할 배열 Stack stack = new Stack(); for (int i = 0; i numbers[stack.peek()]) { result[stack.pop()] = numbers[i]; // 뒷 큰수를 결과 배열에 저장 } stack.push(i); // 현재 인덱스를 스택..
import java.util.*; class Solution { public boolean solution(String[] phone_book) { Map map = new HashMap(); for (int i=0; i
import java.util.*; class Solution { public int solution(String str1, String str2) { str1 = str1.toLowerCase(); str2 = str2.toLowerCase(); Map map1 = new HashMap(); Map map2 = new HashMap(); for (int i = 0; i < str1.length() - 1; i++) { char first = str1.charAt(i); char second = str1.charAt(i + 1); if (Character.isLetter(first) && Character.isLetter(second)) { map1.put(str1.substring(i, i + 2), ..
import java.util.*; class Solution { public int solution(int[] priorities, int location) { // 우선순위 큐를 내림차순 정렬로 설정한다. PriorityQueue pq = new PriorityQueue(Collections.reverseOrder()); int answer = 0; for (int i = 0; i < priorities.length; i++) { pq.add(priorities[i]); } while (!pq.isEmpty()) { for (int i = 0; i < priorities.length; i++) { // priorities의 값이 현재 오름차순 우선순위가 // 제일 높은 값과 같은지 확인한다. if (..
import java.util.*; class Solution { static boolean[] visited; static int count = 0; public int solution(int k, int[][] dungeons) { visited = new boolean[dungeons.length]; dfs(0, k, dungeons); return count; } private void dfs(int depth, int fatigue, int[][] dungeons){ for (int i = 0; i fatigue) { continue; } visited[i] = true; dfs(depth..