본문 바로가기

Algorithm118

[Algorithm / Programmers] 하샤드 수 [Version] ⦁ 2024.04.01 / [Algorithm / Programmers] 하샤드 수 class Solution { public boolean solution(int x) { String s = String.valueOf(x); String[] digits = s.split(""); int sum = 0; for(String digit : digits) { sum += Integer.parseInt(digit); } return x % sum == 0; } } 2024. 4. 1.
[Algorithm / Programmers] 정수 내림차순으로 배치하기 [Version] ⦁ 2024.04.01 / [Algorithm / Programmers] 정수 내림차순으로 배치하기 import java.util.Arrays; class Solution { public long solution(long n) { String s = String.valueOf(n); String[] input = s.split(""); Arrays.sort(input, (s1, s2) -> Integer.compare(Integer.parseInt(s2), Integer.parseInt(s1))); StringBuilder sb = new StringBuilder(); for(String digit : input) { sb.append(digit); } return Long.parseL.. 2024. 4. 1.
[Algorithm / Programmers] 정수 제곱근 판별 [Version] ⦁ 2024.04.01 / [Algorithm / Programmers] 정수 제곱근 판별 class Solution { public long solution(long n) { if(Math.pow(Math.sqrt(n) + 1, 2) % 1 == 0) { return (long) Math.pow(Math.sqrt(n) + 1, 2); } return -1; } } 2024. 4. 1.
[Algorithm / Programmers] 없는 숫자 더하기 [Version] ⦁ 2024.04.01 / [Algorithm / Programmers] 없는 숫자 더하기 import java.util.*; class Solution { public int solution(int[] numbers) { Set uniqueNumbers = new HashSet(); for (int number : numbers) { uniqueNumbers.add(number); } int answer = 0; for (int i = 0; i 2024. 4. 1.
[Algorithm / Programmers] 문자열 내 p와 y의 개수 [Version] ⦁ 2024.04.01 / [Algorithm / Programmers] 문자열 내 p와 y의 개수 class Solution { boolean solution(String s) { s = s.toUpperCase(); // 문자열을 모두 대문자로 변환 int pCount = 0; int yCount = 0; for(int i = 0; i < s.length(); i++) { char c = s.charAt(i); if(c == 'P') { pCount++; } else if(c == 'Y') { yCount++; } } return pCount == yCount; } } 2024. 4. 1.
[Algorithm / Programmers] 자연수 뒤집어 배열로 만들기 [Version] ⦁ 2024.04.01 / [Algorithm / Programmers] 자연수 뒤집어 배열로 만들기 class Solution { public int[] solution(long n) { String input = String.valueOf(n); int[] answer = new int[input.length()]; for (int i = input.length() - 1; i >= 0; i--) { char digitChar = input.charAt(i); int digit = Character.getNumericValue(digitChar); answer[input.length() - 1 - i] = digit; } return answer; } } 2024. 4. 1.
[Algorithm / Programmers] K번째 수 [Version] ⦁ 2024.03.21 / [Algorithm / Programmers] K번째 수 import java.util.*; class Solution { public int[] solution(int[] array, int[][] commands) { int[] answer = new int[commands.length]; for(int i = 0; i < commands.length; i++) { int[] command = commands[i]; int start = command[0] - 1; int end = command[1]; int pos = command[2]; List list = new ArrayList(); for(int j = start; j < end; j++) { .. 2024. 3. 21.
[Algorithm / Programmers] 체육복 [Version] ⦁ 2024.03.21 / [Algorithm / Programmers] 체육복 import java.util.*; class Solution { public int solution(int n, int[] lost, int[] reserve) { Arrays.sort(reserve); Arrays.sort(lost); int answer = n - lost.length; for(int i = 0; i < lost.length; i++) { for(int j = 0; j < reserve.length; j++) { if(lost[i] == reserve[j]) { answer++; lost[i] = -1; reserve[j] = -1; break; } } } for(int i = 0; i.. 2024. 3. 21.
[Algorithm / Programmers] 게임 맵 최단거리 [Version] ⦁ 2024.03.21 / [Algorithm / Programmers] 게임 맵 최단거리 import java.util.*; class Solution { public static int[] dx = {1, 0, -1, 0}; public static int[] dy = {0, 1, 0, -1}; public int solution(int[][] maps) { int answer = BFS(maps); return answer; } public static int BFS(int[][] maps) { int n = maps.length; int m = maps[0].length; boolean[][] visited = new boolean[n][m]; Queue queue = new L.. 2024. 3. 21.