본문 바로가기

java257

[Programmers] Lv.0 / 세균 증식 / Java 문제 풀이 class Solution { public int solution(int n, int t) { return n * (int) Math.pow((double) 2, (double) t); } } class Solution { public int solution(int n, int t) { for(int i = 0; i < t; i++) { n *= 2; } return n; } } class Solution { public int solution(int n, int t) { // 비트 연산자를 사용하여 정수 n을 t번 왼쪽으로 시프트(shift)한 후 결과를 반환 int answer = n 2024. 4. 24.
[Programmers] Lv.0 / 문자열 정렬하기 (2) / Java 문제 풀이 import java.util.Arrays; class Solution { public String solution(String my_string) { String lower_case = my_string.toLowerCase(); char[] char_array = lower_case.toCharArray(); Arrays.sort(char_array); return new String(char_array); } } 출처 https://school.programmers.co.kr/learn/courses/30/lessons/120911 2024. 4. 24.
[Programmers] Lv.0 / 7의 개수 / Java 문제 풀이 import java.util.Arrays; class Solution { public int solution(int[] array) { int count = 0; for (int num : array) { while (num > 0) { if (num % 10 == 7) { count++; } num /= 10; } } return count; } } 출처 https://school.programmers.co.kr/learn/courses/30/lessons/120912 2024. 3. 25.
인스턴스 생성과 정적 팩토리 메서드 인스턴스 생성자바에서 객체를 생성하는 기본적인 방법은 new 키워드를 사용하여 생성자를 호출하는 방식가장 직관적이고 간단한 방법예시public class Car { private String model; private int year; // 생성자 public Car(String model, int year) { this.model = model; this.year = year; } public void displayInfo() { System.out.println("Model: " + model + ", Year: " + year); }}// 인스턴스 생성public class Main { public static void.. 2024. 3. 25.
Java Validation, Spring Validation Java Validation (JSR-303/JSR-380)Java Bean Validation 표준을 따름객체의 필드에 대해 유효성 검사를 선언적으로 설정할 수 있음주요 애노테이션(@NotNull, @Size, @Email 등)을 사용하여 필드의 제약 조건을 정의하고, 이를 Validator를 사용하여 실행할 수 있음주요 특징객체의 각 필드에 유효성 검사를 적용하는 애노테이션 기반의 방법@NotNull, @Size, @Min, @Max, @Email 등 다양한 기본 애노테이션 제공커스텀 유효성 검사 가능Java SE 환경에서도 사용 가능하며, Spring과 같은 다른 프레임워크와 통합 가능Java Validation 샘플 코드import javax.validation.constraints.NotNull.. 2024. 3. 25.
[Programmers] Lv.0 / 잘라서 배열로 저장하기 / Java 문제 풀이 import java.util.ArrayList; import java.util.List; class Solution { public String[] solution(String my_str, int n) { List resultList = new ArrayList(); int length = my_str.length(); for(int i = 0; i < length; i += n) { resultList.add(my_str.substring(i, Math.min(i + n, length))); } return resultList.toArray(new String[0]); } } 출처 https://school.programmers.co.kr/learn/courses/30/lessons/12.. 2024. 3. 20.
[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.