본문 바로가기

java257

[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.
[Programmers] Lv.0 / 그림 확대 / Java 문제 풀이 import java.util.Arrays; class Solution { public String[] solution(String[] picture, int k) { int rows = picture.length; int cols = picture[0].length(); // 조건 : 모든 picture의 원소의 길이는 같습니다. String[] enlargedPicture = new String[rows * k]; for (int i = 0; i < rows * k; i++) { StringBuilder row = new StringBuilder(); for (int j = 0; j < cols * k; j++) { char pixel = picture[i / k].charAt(j / k).. 2024. 3. 7.
[Programmers] Lv.0 / 커피 심부름 / Java 문제 풀이 import java.util.Arrays; class Solution { public int solution(String[] order) { return Arrays.stream(order) .mapToInt(x -> x.contains("cafelatte") ? 5000 : 4500) .sum(); } } class Solution { public int solution(String[] order) { int answer = 0; for(String str : order) { if(str.contains("cafelatte")) { answer += 5000; } else { answer += 4500; } } return answer; } } 출처 https://school.program.. 2024. 3. 7.
[Programmers] Lv.0 / 날짜 비교하기 / Java 문제 풀이 import java.time.LocalDate; class Solution { public int solution(int[] date1, int[] date2) { LocalDate localDate1 = LocalDate.of(date1[0], date1[1], date1[2]); LocalDate localDate2 = LocalDate.of(date2[0], date2[1], date2[2]); return localDate1.compareTo(localDate2) >= 0 ? 0 : 1; } } localDate1.compareTo(localDate2) 호출한 LocalDate 객체가 localDate2 보다 빠른 경우, 음수 반환 호출한 LocalDate 객체가 localDate2 .. 2024. 3. 6.
[Programmers] Lv.0 / 주사위 게임 1 / Java 문제 풀이 class Solution { public int solution(int a, int b) { if(a % 2 == 1 && b % 2 == 1) { return (int) (Math.pow(a, 2) + Math.pow(b, 2)); } else if(a % 2 == 1 || b % 2 == 1) { return 2 * (a + b); } else { return Math.abs(a - b); } } } Math.pow(double a, double b) 출처 https://school.programmers.co.kr/learn/courses/30/lessons/181839 2024. 3. 6.
[Programmers] Lv.0 / 정수 찾기 / Java 문제 풀이 import java.util.stream.IntStream; class Solution { public int solution(int[] num_list, int n) { return IntStream.of(num_list).anyMatch(x -> x == n) ? 1 : 0; } } Arrays.asList(num_list).contains(n) 조건을 사용할 수 없던 이유 Arrays.asList()는 기본 배열을 단일 요소로 취급함. 예를 들어, 입력값이 [1, 2, 3, 4, 5]와 같이 정수 배열이 주어졌을 때, Arrays.asList()는 이를 단일 요소로 간주하고 크기가 1인 리스트를 생성함. 즉, 리스트에는 [1, 2, 3, 4, 5]라는 하나의 요소가 포함됨 Java에서 .. 2024. 3. 6.
[Programmers] Lv.0 / 꼬리 문자열 / Java 문제 풀이 import java.util.Arrays; import java.util.stream.Collectors; class Solution { public String solution(String[] str_list, String ex) { return Arrays.stream(str_list) .filter(str -> !str.contains(ex)) .collect(Collectors.joining()); } } 출처 https://school.programmers.co.kr/learn/courses/30/lessons/181841 2024. 3. 5.