Algorithm/LeetCode
-
[linked list] add two numbersAlgorithm/LeetCode 2025. 4. 20. 11:40
✅ Problemhttps://leetcode.com/problems/add-two-numbers/description/ ✅ Approach & Solution방식) 반복문 사용더보기반복문을 사용해 첫 번째 연결 리스트, 두 번째 연결 리스트, 자리올림수를 모두 처리할 때까지 반복dummyNode 생성하여 연결 리스트 구현 (null 확인 로직 줄이기 위함)/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNo..
-
[linked list] reverse linked listAlgorithm/LeetCode 2025. 4. 20. 10:45
✅ Problemhttps://leetcode.com/problems/reverse-linked-list/description/ ✅ Approach & Solution방식1) iterative더보기현재 노드의 next가 이전 노드여야 한다./** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */class Solution..
-
[hash] jewels and stonesAlgorithm/LeetCode 2025. 4. 8. 09:52
✅ Problemhttps://leetcode.com/problems/jewels-and-stones/description/ ✅ Approach & Solution방식) HashSet 사용더보기class Solution { public int numJewelsInStones(String jewels, String stones) { int cnt = 0; Set jSet = new HashSet(); for (Character c : jewels.toCharArray()) { jSet.add(c); } for (Character c : stones.toCharArray()) { if (..
-
[array] best time to buy and sell stock 2Algorithm/LeetCode 2025. 4. 7. 09:43
✅ Problemhttps://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/ ✅ Approach & Solution방식) greedy 사용더보기조건최대 1개의 주식만 가지고 있을 수 있다.같은 날에 주식을 사고 팔 수 있다.최대 수익을 내려면 현재 값이 이전 값보다 큰 경우 무조건 팔아야 한다.class Solution { public int maxProfit(int[] prices) { int maxProfit = 0; for (int i = 1; i prices[i-1]) { maxProfit += (prices[i] - prices[i-1]); ..