[LeetCode#21] Merge Two Sorted Lists
·
Algorithm/문제풀이
❒ Description제목Merge Two Sorted Lists링크https://leetcode.com/problems/merge-two-sorted-lists/description/자료구조선형 자료구조푼 날짜7/24     ❒ Solution1. Brute Force public static void main(String[] args) { // create Test Case ListNode node1_3 = new ListNode(4, null); ListNode node1_2 = new ListNode(2, node1_3); ListNode node1_1 = new ListNode(1, node1_2); ListNode node2_3 = new ListNode(4, nu..
[LeetCode#234] Palindrome Linked List
·
Algorithm/문제풀이
❒ Description제목Palindrome Linked List링크https://leetcode.com/problems/palindrome-linked-list/description/자료구조선형자료 구조 (연결 리스트)푼 날짜7/24    ❒ Solution1. Deque 자료구조를 사용한 풀이public static boolean solve(ListNode head) { Deque deque = new LinkedList(); ListNode node = head; while (node != null) { deque.add(node.val); node = node.next; } while (!deque.isEmpty() && deque.size()..
[LeetCode#121] Best Time to Buy and Sell Stock
·
Algorithm/문제풀이
❒ Description제목Best Time to Buy and Sell Stock링크https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/자료구조선형 자료구조풀 날짜07/24    ❒ Solutionpublic static int maxProfit(int[] prices) { int maxProfit = 0; int minPrice = prices[0]; for (int price : prices) { minPrice = Math.min(minPrice, price); maxProfit = Math.max(maxProfit, price - minPrice); } return..
[LeetCode#238] Product of Array Except Self
·
Algorithm/문제풀이
❒ Description제목Product of Array Except Self링크https://leetcode.com/problems/product-of-array-except-self/description/자료구조선형 자료구조푼 횟수7/23 이번 문제는 나누기를 하지말라는 제한 조건과 시간복작도 O(n)을 넘기지 말라는 요구사항이 있었다.   ❒ Solutionpublic static int[] solve(int[] nums) { int[] result = new int[nums.length]; int p = 1; for (int i = 0; i = 0; i--) { result[i] *= p; p *= nums[i]; } return result;}..
[LeetCode#15] 3Sum
·
Algorithm/문제풀이
❒ Description 제목3Sum링크https://leetcode.com/problems/3sum/description/자료구조선형 자료구조시간복잡도Two Pointer 풀이 - O(n²) 선형 자료구조 문제로, two pointer 알고리즘을 사용하여 해결할 수 있는 문제.이 문제는 중복 값에 대한 적절한 처리를 해줘야 하는 점이 까다로웠다. ❒ Solutionpublic static List> solve(int[] nums) { int left, right, total; List> res = new ArrayList(); Arrays.sort(nums); for (int i = 0; i 0 && nums[i] == nums[i - 1]) { continu..
[LeetCode#42] Trapping Rain Water
·
Algorithm/문제풀이
❒ Description 제목Trapping Rain Water링크https://leetcode.com/problems/trapping-rain-water/description/자료구조선형 자료구조   ❒ Solution 1. Two Pointerpublic static int twoPointerSolve(int[] height) { int leftPointer = 0; int rightPointer = height.length - 1; int leftMax = height[leftPointer]; int rightMax = height[height.length - 1]; int water = 0; while (leftPointer    2. Stackpublic stat..
[LeetCode#1] Two Sum
·
Algorithm/문제풀이
❒ Description제목 : Two Sum링크 : https://leetcode.com/problems/two-sum/description/카테고리 : 선형 자료구조 중 배열 문제에 속한다.주어진 배열 중 2개의 요소의 합이 target이 될 때, 각 요소의 index를 return하는 문제   ❒ Solution 1. Brute Forcepublic static int[] solve1(int[] nums, int target) { int n = nums.length; for (int i = 0; i 시간 복잡도 : O(n²)모든 경우를 전부 확인하는 방법 2. Two-pass Hash Table▶︎ Two-pass Hash Table주로 두 번의 탐색을 통해 문제를 해결하는 알고리즘에서 ..