[LeetCode] Grind 75 questions (12/75) Linked List Cycle

Grind75 문제로 풀이 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public boolean hasCycle(ListNode head) { if(head == null || head.next == null) return false; ListNode slow = head; ListNode fast = head.next; while(slow != fast) { if(fast == null || fast.next == null) return false; slow = slow.next; fast = fast.next.next; } return true; } } ...

July 8, 2024 · 김태영

[LeetCode] Grind 75 questions (13/75) Implement Queue using Stacks

Grind75 문제로 풀이 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 class MyQueue { private Stack<Integer> stack1; private Stack<Integer> stack2; public MyQueue() { stack1 = new Stack<>(); stack2 = new Stack<>(); } public void push(int x) { stack1.push(x); } public int pop() { if (stack2.isEmpty()) { while (!stack1.isEmpty()) { stack2.push(stack1.pop()); } } return stack2.pop(); } public int peek() { if (stack2.isEmpty()) { while (!stack1.isEmpty()) { stack2.push(stack1.pop()); } } return stack2.peek(); } public boolean empty() { return stack1.isEmpty() && stack2.isEmpty(); } } /** * Your MyQueue object will be instantiated and called as such: * MyQueue obj = new MyQueue(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.peek(); * boolean param_4 = obj.empty(); */ ...

July 8, 2024 · 김태영

[LeetCode] Grind 75 questions (10/75) Lowest Common Ancestor of a Binary Search Tree

Grind75 문제로 풀이 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root.val > p.val && root.val > q.val) { // root보다 값이 작을 경우 return lowestCommonAncestor(root.left, p, q); } else if(root.val < p.val && root.val < q.val) { // root보다 값이 클 경우 return lowestCommonAncestor(root.right, p, q); } else { // TreeNode p와 q가 같은 노드에 없다면 root가 부모다 return root; } } }

July 6, 2024 · 김태영

월급쟁이

layout: post title: 월급쟁이 #image: path: /assets/img/blog/jeremy-bishop@0,5x.jpg description: > sitemap: false 같은 월급쟁이들끼리… 사정 아시잖아요 최근 우리팀에게 프로젝트 제안을 보내는 현업팀에서 자주 하는 멘트다. 6월 중순까지도 협의되지 않은 요건이었지만 ‘가능하다면’ 7월 초 테스트 오픈이었다. ...

July 6, 2024 · 김태영

[LeetCode] Grind 75 questions (7/75) Valid Anagram

Grind75 문제로 풀이 1 2 3 4 5 6 7 8 9 10 11 class Solution { public boolean isAnagram(String s, String t) { char[] c1 = s.toCharArray(); char[] c2 = t.toCharArray(); Arrays.sort(c1); Arrays.sort(c2); return new String(c1).equals(new String(c2)); } } 풀이2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class Solution { public boolean isAnagram(String s, String t) { if (s.length() != t.length()) { return false; } Map<Character, Integer> count = new HashMap<>(); for(char c : s.toCharArray()) { count.put(c, count.getOrDefault(c, 0) + 1); } for(char c : t.toCharArray()) { count.put(c, count.getOrDefault(c, 0) -1); } for(int n : count.values()) { if(n != 0) return false; } return true; } }

July 2, 2024 · 김태영

[LeetCode] Grind 75 questions (8/75) Binary Search

Grind75 문제로 풀이 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class Solution { public int search(int[] nums, int target) { int left = 0; int right = nums.length-1; while(left <= right) { int mid = left + (right-left) / 2; if(nums[mid] == target) return mid; else if(nums[mid] > target) { right = mid - 1; } else { left = mid + 1; } } return -1; } }

July 2, 2024 · 김태영

[LeetCode] Grind 75 questions (9/75) Flood Fill

Grind75 문제로 풀이 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 class Solution { public int[][] floodFill(int[][] image, int sr, int sc, int color) { int startColor = image[sr][sc]; // 1. 시작 색상 저장 if (startColor == color) return image; // 기저 조건: 색상이 같으면 변경 불필요 fill(image, sr, sc, startColor, color); // 4. 재귀 호출로 플러드 필 시작 return image; } private void fill(int[][] image, int sr, int sc, int startColor, int newColor) { // 2. 기저 조건 설정: 범위를 벗어나거나 현재 픽셀의 색상이 시작 색상과 다르면 재귀 호출을 종료합니다. if (sr < 0 || sr >= image.length || sc < 0 || sc >= image[0].length || image[sr][sc] != startColor) { return; } // 3. 색상 변경 image[sr][sc] = newColor; // 4. 재귀 호출: 4방향(상하좌우)으로 인접한 픽셀에 대해 재귀적으로 fill 메서드를 호출하여 동일한 작업을 반복합니다. fill(image, sr + 1, sc, startColor, newColor); // 아래로 이동 fill(image, sr - 1, sc, startColor, newColor); // 위로 이동 fill(image, sr, sc + 1, startColor, newColor); // 오른쪽으로 이동 fill(image, sr, sc - 1, startColor, newColor); // 왼쪽으로 이동 } }

July 2, 2024 · 김태영

[LeetCode] Grind 75 questions (3/75) Merge Two Sorted Lists

Grind75 문제로 풀이 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 /** * 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 { public ListNode mergeTwoLists(ListNode list1, ListNode list2) { ListNode result = new ListNode(); ListNode dummy = result; // list를 비교하여 dummy에 적재한다. while(list1 != null && list2 != null) { if(list1.val > list2.val) { dummy.next = list2; list2 = list2.next; } else { dummy.next = list1; list1 = list1.next; } dummy = dummy.next; } // 두 리스트 중 하나가 null이 되었으므로 남은 lsit를 next에 적재한다. if(list1 == null) dummy.next = list2; else dummy.next = list1; return result.next; // 초기값은 null이므로 next부터 반환한다. } } ...

July 1, 2024 · 김태영

[LeetCode] Grind 75 questions (4/75) Best Time to Buy and Sell Stock

Grind75 문제로 풀이 1 2 3 4 5 6 7 8 9 10 11 12 class Solution { public int maxProfit(int[] prices) { int min = Integer.MAX_VALUE; int profit = 0; for(int price : prices) { if(min > price) min = price; if(price - min > profit) profit = price - min; } return profit; } }

July 1, 2024 · 김태영

[LeetCode] Grind 75 questions (5/75) Valid Palindrome

Grind75 문제로 풀이 1 2 3 4 5 6 7 8 9 class Solution { public boolean isPalindrome(String s) { s = s.toLowerCase().replaceAll("[^a-z0-9]", ""); int point = s.length() / 2; StringBuffer sb = new StringBuffer(s.substring(s.length() - point)); return s.substring(0,point).equals(sb.reverse().toString()); } } 풀이 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Solution { public boolean isPalindrome(String s) { s = s.toLowerCase().replaceAll("[^a-z0-9]", ""); int left = 0; int right = s.length() - 1; while(left < right) { if(s.charAt(left++) != s.charAt(right--)) return false; } return true; } }

July 1, 2024 · 김태영