[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부터 반환한다. } } ...

2024년 7월 1일 · 김태영

[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; } } ...

2024년 7월 1일 · 김태영

[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; } } ...

2024년 7월 1일 · 김태영

[LeetCode] Grind 75 questions (6/75) Invert Binary 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 25 26 27 28 29 30 31 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public TreeNode invertTree(TreeNode root) { if(root == null) return root; return invert(root); } private TreeNode invert(TreeNode node) { if(node == null) return node; TreeNode temp = node.left; node.left = node.right; node.right = temp; invert(node.left); invert(node.right); return node; } } ...

2024년 7월 1일 · 김태영

[LeetCode] Grind 75 questions (2/75) Valid Parentheses

Grind75 문제로 풀이 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Solution { public boolean isValid(String s) { Stack<Character> stack = new Stack<>(); for(int i = 0; i < s.length(); i++) { if(s.charAt(i) == '(') { stack.push(')'); } else if (s.charAt(i) =='{') { stack.push('}'); } else if (s.charAt(i) == '[') { stack.push(']'); } else { if(stack.isEmpty()) return false; char c = stack.pop(); if(c != s.charAt(i)) return false; } } return stack.isEmpty(); } } ...

2024년 6월 30일 · 김태영

[LeetCode] Grind 75 questions (1/75) Two Sum

Grind75{: target="_blank"} 문제로{: target="_blank"} 0초로 푼 이들의 답이 궁금하다 풀이1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Solution { public int[] twoSum(int[] nums, int target) { if(nums == null || nums.length == 0) return null; for(int i = 0; i < nums.length -1; i++) { for(int j = i+1; j < nums.length; j++) { if(nums[i] + nums[j] == target){ int[] arr = {i, j}; return arr; } } } return null; } } ...

2024년 6월 29일 · 김태영

LeetCode Grind 75 — 문제 목록과 풀이 계획

leetcode를 매일 1문제씩 푸는게 객단가가 안나온다 느껴져서 조언을 구했다. 최빈출 75제 먼저 정복하고 빈도수별로 풀어나가라고 답변을 얻었다. 끝. 최빈출 75제 (https://www.techinterviewhandbook.org/grind75) ...

2024년 6월 29일 · 김태영

[LeetCode] 110. Balanced Binary Tree (Easy)

문제로 못풀었다. 재귀로 푸는 패턴에 익숙해져야겠다. 정답 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 a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public boolean isBalanced(TreeNode root) { return checkHeight(root) != -1; } private int checkHeight(TreeNode node) { if (node == null) return 0; int leftHeight = checkHeight(node.left); if (leftHeight == -1) return -1; int rightHeight = checkHeight(node.right); if (rightHeight == -1) return -1; if (Math.abs(leftHeight - rightHeight) > 1) return -1; return Math.max(leftHeight, rightHeight) + 1; } }

2024년 6월 27일 · 김태영

[LeetCode] 108. Convert Sorted Array to Binary Search Tree (Easy)

문제로 못풀었다. 정답 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 TreeNode sortedArrayToBST(int[] nums) { if(nums == null || nums.length == 0) { return null; } return convertToBST(nums, 0, nums.length -1); } public TreeNode convertToBST(int[] nums, int left, int right) { if(left > right) { // 종료됬음을 의미 return null; } int mid = left + (right - left) / 2; TreeNode node = new TreeNode(nums[mid]); node.left = convertToBST(nums, left, mid-1); node.right = convertToBST(nums, mid+1, right); return node; } } 참고 1 int mid = left + (right - left) / 2; 정수 오버플로우를 방지하기 위해서 위와 같은 방식으로 중간 인덱스를 계산한다. ...

2024년 6월 26일 · 김태영

친절한 SQL 튜닝 독서 정리

1회독을 마칠때쯤 팀에서 신규 프로젝트의 데이터를 어떤 테이블에 적재할지 논의하는 시간이 생겼다. ‘A 테이블 인덱스 문제로 B테이블에 거래내역을 적재해야 합니다.’ ...

2024년 6월 26일 · 김태영