본문 바로가기

Coding Test/프로그래머스

(138)
[프로그래머스/자바] 푸드 파이트 대회 class Solution { public String solution(int[] food) { StringBuilder sb = new StringBuilder(); for (int i = 1; i < food.length; i++) { for (int j = 0; j < food[i] / 2; j++) { sb.append(i); } } return sb.toString() + "0" + sb.reverse().toString(); } }
[프로그래머스/자바] 쿼드압축 후 개수 세기 class Solution { private static int[] answer = new int[2]; public int[] solution(int[][] arr) { dq(0, 0, arr.length, arr); return answer; } private static void dq(int dx, int dy, int size, int[][] arr) { if (check(dx, dy, size, arr)) { answer[arr[dx][dy]]++; return; } dq(dx, dy, size / 2, arr); dq(dx + size / 2, dy, size / 2, arr); dq(dx, dy + size / 2, size / 2, arr); dq(dx + size / 2, dy + siz..
[프로그래머스/자바] 신규 아이디 추천 class Solution { public String solution(String new_id) { new_id = new_id.toLowerCase(); new_id = new_id.replaceAll("[^0-9a-z\\-_.]", ""); new_id = new_id.replaceAll("\\.+", "."); new_id = new_id.replaceAll("^\\.+|\\.+$", ""); if (new_id.isEmpty()) new_id = "a"; if (new_id.length() >= 16) new_id = new_id.substring(0, 15); new_id = new_id.replaceAll("\\.+$", ""); while (new_id.length() < 3) { new_..
[프로그래머스/자바] 문자열 내 p와 y의 개수 class Solution { boolean solution(String s) { s = s.toLowerCase(); char[] list = s.toCharArray(); int p = 0; int y = 0; for(char c : list) { if (c == 'p') p++; else if (c == 'y') y++; } return p == y; } }
[프로그래머스/자바] 문자열 압축 import java.util.*; class Solution { public int solution(String s) { int answer = Integer.MAX_VALUE; StringBuilder sb = new StringBuilder(); for (int i = 1; i s.length()) list.add(s.substring(j, s.length())); else list.add(s.substring(j, j + i)); } String last = ""; int count = 0; for (String value : list) { if (last.equals(value)) { count++; } else { if (count > 1) sb.append(count); sb.append(l..
[프로그래머스/자바] 이상한 문자 만들기 class Solution { public String solution(String s) { StringBuilder sb = new StringBuilder(); boolean upperFlag = true; for (char c : s.toCharArray()){ if (!Character.isAlphabetic(c)) { sb.append(c); upperFlag = true; } else { if (upperFlag) { sb.append(Character.toUpperCase(c)); } else sb.append(Character.toLowerCase(c)); upperFlag = !upperFlag; } } return sb.toString(); } }
[프로그래머스/자바] 시저 암호 class Solution { public String solution(String s, int n) { StringBuilder sb = new StringBuilder(); for (char c : s.toCharArray()) { if (!Character.isAlphabetic(c)) { sb.append(c); continue; } char target = c
[프로그래머스/자바] 거리두기 확인하기 import java.util.*; class Solution { class Point { int row, col, dtc; Point(int row, int col, int dtc) { this.row = row; this.col = col; this.dtc = dtc; } } private static final int[][] D = {{1,0}, {-1,0}, {0,1}, {0,-1}}; private boolean bfs(String[] place, int x, int y) { boolean[][] visited = new boolean[5][5]; Queue q = new LinkedList(); visited[x][y] = true; q.add(new Point(x, y, 0)); while ..