본문 바로가기

분류 전체보기140

[Java] 애노테이션(Annotation)과 리플렉션(Reflection)을 통한 애노테이션 활용 정리 언젠간 한번 정리하려고 했지만 이제서야 하게 됐다.. 이미 만들어진 애노테이션을 많이 사용하기는 했는데 어떤방식으로 돌아가는지, 특히 런타임 중에 애노테이션이 달린 코드를 어떻게 찾아서 특정 기능을 수행하는지는 잘 몰랐다. 이번 포스팅에서는 애노테이션의 개념과 주로 활용되는 예시를 함께 정리하려고 한다. 애노테이션이란? 애노테이션은 프로그램의 코드에 추가하는 메타데이터로, 컴파일러나 런타임 시 코드를 처리하는 도구들에게 어떻게 처리해야 할지 추가적인 정보를 제공한다. 애노테이션은 Retention에 따라 다음 세 가지 용도로 활용된다. 1. 컴파일러에게 필요한 정보 제공 2. 빌드 툴이 코드를 자동으로 생성할 때 사용하는 정보 제공 3. 런타임 시 특정 기능을 처리할 때 사용하는 정보 제공(with Re.. 2024. 3. 17.
[Java] TreeSet에 중복된 값을 넣어야한다면? 결론부터 말해서.. TreeSet의 중복 요소를 넣고 싶다면 Comparable한 객체를 만들어 해당 타입을 갖도록 한다. compareTo를 재정의 할 때 같은 값을 갖는 경우 0이 아닌 다른 값을 리턴하도록 한다. 그런데 중복된 값을 가지는 SortedCollection이 필요하다면 TreeMap을 사용하는 게 더 나아보인다. value를 키로 갖고 중복된 요소의 개수를 value로 갖는 TreeMap을 만들면 위의 1, 2를 굳이 구현할 필요가 없기 때문이다. 작년에 풀었던 이중 우선순위 큐를 다시 풀면서 TreeSet으로 구현하게 되었는데, 몇가지 간과한 부분 때문에 오답처리를 받게 됐다. 해당 특징과 더 나은 방법을 정리해 비슷한 유형에서 같은 실수를 하지 않도록 하자.. 2023.05.03 .. 2024. 3. 15.
LeetCode 334. Increasing Triplet Subsequence [Java] https://leetcode.com/problems/increasing-triplet-subsequence/description/ 시나리오 a와b를 Integer.MAX_VALUE로 초기화한다. 배열을 순회하며 현재 요소가 a보다 작거나 같으면 현재 요소를 a에 갱신. 현재 요소가 a보다는 크나 b보다는 작거나 같으면 b 갱신. a와 b 둘보다 크면 배열 내 triplet subsequence가 존재한다는 것이므로 return true; 순회 완료 전까지 return 되지 않으면 return false해준다. 코드 class Solution { public boolean increasingTriplet(int[] nums) { if (nums.length < 3) { return false; } int.. 2024. 2. 5.
LeetCode 238. Product Of Array Except Self https://leetcode.com/problems/product-of-array-except-self [LeetCode - The World&#39;s Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com](https://leetcode.com/problems/product-of-array-except-self) 내 코드 class Solution { public int[] productExceptSe.. 2024. 2. 4.
LeetCode 1071. Greatest Common Divisor of Strings [Java] https://leetcode.com/problems/greatest-common-divisor-of-strings/description/?envType=study-plan-v2&envId=leetcode-75 입력으로 주어지는 두 문자열의 최대공약수(GCD)를 구하는 문제이다. 두 문자열의 최대공약수란 두 문자열 내 겹치는 가장 작은 덩어리를 의미한다. 예를 들어 ABABAB ABAB라는 두 문자열이 있을 때 이때의 gcd는 AB가 된다. 내 코드 /** 시나리오 1. str1과 str2의 길이 비를 구함 2. 구한 길이 비를 이용해 각 str의 첫번째 덩어리를 구하고(ex: 길이 비==3이면 해당 문자열을 3덩어리로 쪼갬) 나머지 덩어리도 첫번째 덩어리와 일치하는지 구함 3. 각 str의 위 조건이 .. 2024. 2. 4.
[Java]백준 1987 - 알파벳 https://www.acmicpc.net/problem/1987 최초 코드 - 메모리 초과 발생 import java.util.*; import java.io.*; class Main { private static int[] dy = {1, 0, -1, 0}; private static int[] dx = {0, 1, 0, -1}; private static char[][] grid; private static int R; private static int C; private static int answer; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputS.. 2024. 1. 24.
[Java] 백준 20922 - 겹치는 건 싫어 (투포인터) https://www.acmicpc.net/problem/20922 정답 코드 import java.util.*; import java.io.*; class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] NK = br.readLine().split(" "); int N = Integer.parseInt(NK[0]); int K = Integer.parseInt(NK[1]); String[] seqStr = br.readLine().split(" "); //mapToInt는 IntStre.. 2024. 1. 24.
[Java] 백준 14719 - 빗물 https://www.acmicpc.net/problem/14719 정답 코드 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] size = br.readLine().split(" "); int answer = 0; int H = Integer.parseInt(size[0]); int W = Integer.parse.. 2024. 1. 23.
[Java]프로그래머스 - 인사고과 https://school.programmers.co.kr/learn/courses/30/lessons/152995 최초 코드 import java.util.*; class Solution { private int[] biggestA={0,0}; private int[] biggestB={0,0}; public int solution(int[][] scores) { //1 calcBiggestOne(scores); //2. enQueue PriorityQueue pq = new PriorityQueue(); for(int i=0; ibiggestB[1]){ biggestB=scores[i]; } } } private int calcRank(PriorityQueue pq){ int cnt=0; int ra.. 2024. 1. 20.