본문 바로가기

Java66

[Java] 프로그래머스 [Level-1] 햄버거 만들기 public class Solution { // 햄버거 만들기 // 빵(1), 야채(2), 고기(3), 빵(1)일 경우 햄버거 완성 public static int solution(int[] ingredient) { int answer = 0; StringBuilder sb = new StringBuilder(); for (int i = 0; i 3 && sb.subSequence(sb.length() - 4, sb.length()).equals("1231")) { sb.delete(sb.length() - 4, sb.length()); answer++; } } return a.. 2022. 11. 28.
[Java] 프로그래머스 [Level-2] 주식가격 import java.util.Stack; public class Solution { // 주식가격 public static int[] solution(int[] prices) { int[] answer = new int[prices.length]; Stack stack = new Stack(); for (int i = 0; i pr.. 2022. 11. 28.
[Java] 프로그래머스 [Level-3] 여행경로 import java.util.ArrayList; import java.util.Collections; public class Solution { // 여행경로 // 주어진 항공권을 모두 이용하여 여행경로를 짜려고 합니다. 항상 "ICN" 공항에서 출발합니다. // 주어진 공항 수는 3개 이상 10,000개 이하입니다. // 주어진 항공권은 모두 사용해야 합니다. // 만일 가능한 경로가 2개 이상일 경우 알파벳 순서가 앞서는 경로를 return 합니다. // 모든 도시를 방문할 수 없는 경우는 주어지지 않습니다. static boolean[] visited; static ArrayList travelRouteList; public static void dfs(String start, String rou.. 2022. 11. 28.
[Java] 프로그래머스 [Level-2] [1차] 캐시 import java.util.LinkedList; public class Solution { // [1차] 캐시 // LRU(Least Recently Used) : 가장 오랫동안 참조되지 않은 페이지를 교체하는 방식 // 캐시가 가득 찼을 때, 가장 오랫동안 참조되지 않은 페이지를 찾아서 없애는 과정이 필요 public static int solution(int cacheSize, String[] cities) { int answer = 0; String city = ""; LinkedList cache = new LinkedList(); if (cacheSize == 0) { return cities.length * 5; } for (int i = 0; i < cities.length; i++) {.. 2022. 11. 28.
[Java] 프로그래머스 [Level-3] 자물쇠와 열쇠 public class Solution { // 자물쇠와 열쇠 // key, lock 3이상 20이하 크기의 2차원 배열 // key는 회전과 이동이 가능 public static boolean openLock(int[][] checkArr, int point, int lockLength) { // point : 체크용 배열 checkArr에서 실제 lock의 시작 위치 // lockLength : lock의 길이 for (int i = 0; i < lockLength; i++) { for (int j = 0; j < lockLength; j++) { if (checkArr[point + i][point + j] != 1) return false; // 0 : 채워지지 않은 공간, 2 : 튀어나온 부분끼.. 2022. 11. 28.
[Java] 프로그래머스 [TEST] Uncompress String import java.util.Stack; public class Solution { // 압축 문자열 풀기 public static String solution(String compressed) { String answer = ""; // 최종 문자열을 담을 변수 Stack charStack = new Stack(); // 영단어와 '(' 괄호를 담을 스택 Stack numStack = new Stack(); // 숫자를 담을 스택 String tempStr = ""; // 임시 문자열을 담을 변수 int tempNum = 0; // 임시 숫자를 담을 변수 String repeatStr = ""; // 반복 문자열을 담을 변수 StringBuilder sb = new StringBuilder(); //.. 2022. 11. 28.