codingtest161 [Programmers] Lv.0 / 문자열 밀기 / Java 문제 풀이 class Solution { public int solution(String A, String B) { if(A.equals(B)) { return 0; } int length = A.length(); for(int i = 0; i < length; i++) { A = A.charAt(length - 1) + A.substring(0, length - 1); if(A.equals(B)) { return i + 1; } } return -1; } } class Solution { public int solution(String A, String B) { String tempB = B.repeat(3); return tempB.indexOf(A); } } class Solution { publi.. 2024. 3. 20. [Programmers] Lv.0 / 종이 자르기 / Java 문제 풀이 class Solution { public int solution(int M, int N) { int horizontalCuts = M - 1; int verticalCuts = M * (N - 1); return horizontalCuts + verticalCuts; } } class Solution { public int solution(int M, int N) { return M * N - 1; } } 출처 https://school.programmers.co.kr/learn/courses/30/lessons/120922 2024. 3. 20. [Programmers] Lv.0 / 연속된 수의 합 / Java 문제 풀이 class Solution { public int[] solution(int num, int total) { int first = (total - (num * (num - 1) / 2)) / num; int[] result = new int[num]; for(int i = 0; i < num; i++) { result[i] = first + i; } return result; } } 출처 https://school.programmers.co.kr/learn/courses/30/lessons/120923 2024. 3. 19. [Programmers] Lv.0 / 다음에 올 숫자 / Java 문제 풀이 class Solution { public int solution(int[] common) { int n = common.length; int commonDifferenceOrRatio = common[1] - common[0]; if (common[n - 1] - common[n - 2] == commonDifferenceOrRatio) { return common[n - 1] + commonDifferenceOrRatio; } else { int ratio = common[n - 1] / common[n - 2]; return common[n - 1] * ratio; } } } class Solution { public int solution(int[] common) { int answer.. 2024. 3. 19. [Programmers] Lv.0 / 옹알이 (1) / Java 문제 풀이 import java.util.*; class Solution { public int solution(String[] babbling) { int count = 0; for (String word : babbling) { if (isPossible(word)) count++; } return count; } private boolean isPossible(String word) { if (word.equals("")) return true; if (word.startsWith("aya")) return isPossible(word.substring(3)); if (word.startsWith("ye")) return isPossible(word.substring(2)); if (word.sta.. 2024. 3. 18. [Programmers] Lv.0 / 이차원 배열 대각선 순회하기 / Java 문제 풀이 class Solution { public int solution(int[][] board, int k) { int sum = 0; for(int i = 0; i < board.length; i++) { for(int j = 0; j < board[0].length; j++) { if(i + j 2024. 3. 15. [Programmers] Lv.0 / 정사각형으로 만들기 / Java 문제 풀이 import java.util.Arrays; class Solution { public int[][] solution(int[][] arr) { int rows = arr.length; int cols = 0; for(int[] row : arr) { cols = Math.max(cols, row.length); } int[][] newArr = new int[Math.max(rows, cols)][Math.max(rows, cols)]; for(int i = 0; i < newArr.length; i++) { if(i < arr.length) { for(int j = 0; j < newArr[i].length; j++) { if(j < arr[i].length) { newArr[i][j] =.. 2024. 3. 14. [Programmers] Lv.0 / 특별한 이차원 배열 2 / Java 문제 풀이 class Solution { public int solution(int[][] arr) { int n = arr.length; if(n == 0) { return 1; } if(n != arr[0].length) { return 0; } for(int i = 0; i < n; i++) { for(int j = i + 1; j < n; j++) { if(arr[i][j] != arr[j][i]) { return 0; } } } return 1; } } class Solution { public int solution(int[][] arr) { for(int i = 0; i < arr.length; i++) { for(int j = 0; j < arr[i].length; j++) { if(arr.. 2024. 3. 14. [Programmers] Lv.0 / 정수를 나선형으로 배치하기 / Java 문제 풀이 class Solution { public int[][] solution(int n) { int[][] result = new int[n][n]; int num = 1; int top = 0; int bottom = n - 1; int left = 0; int right = n - 1; while(num = n || answer[nx][ny] != 0) { direction = (direction + 1) % 4; // 범위 밖에 나갔을 때 방향전환 nx = x + dx[direction]; ny = y + dy[direction]; } x = nx; y = ny; } return answer; } } 출처 https://school.programmers.co.kr/learn/courses/3.. 2024. 3. 14. [Programmers] Lv.0 / 특별한 이차원 배열 1 / Java 문제 풀이 class Solution { public int[][] solution(int n) { int[][] arr = new int[n][n]; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { arr[i][j] = (i == j) ? 1 : 0; } } return arr; } } class Solution { public int[][] solution(int n) { int[][] answer = new int[n][n]; for(int i = 0; i < n ; i++) { answer[i][i] = 1; } return answer; } } 출처 https://school.programmers.co.kr/learn/courses/30/l.. 2024. 3. 13. [Programmers] Lv.0 / l로 만들기 / Java 문제 풀이 import java.lang.StringBuilder; class Solution { public String solution(String myString) { StringBuilder result = new StringBuilder(); for(int i = 0; i < myString.length(); i++) { char currentChar = myString.charAt(i); if(currentChar < 'l') { result.append('l'); } else { result.append(currentChar); } } return result.toString(); } } class Solution { public String solution(String myString) {.. 2024. 3. 13. [Programmers] Lv.0 / 조건에 맞게 수열 변환하기 3 / Java 문제 풀이 import java.util.Arrays; class Solution { public int[] solution(int[] arr, int k) { return Arrays.stream(arr) .map(num -> { if(k % 2 == 0) { return num + k; } else { return num * k; } }).toArray(); } } import java.util.*; class Solution { public int[] solution(int[] arr, int k) { return Arrays.stream(arr) .map(operand -> k % 2 == 0 ? operand + k : operand * k) .toArray(); } } import java.u.. 2024. 3. 12. 이전 1 2 3 4 5 6 7 ··· 14 다음