코딩테스트 96

14. Longest Common Prefix

https://leetcode.com/problems/longest-common-prefix/description/ LeetCode - The World'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 /** 1. 문제 해석하기 가장 긴 공통 prefix를 찾는 문제이고 없으면 "" 반환 2. 문제 풀이 방향 이중 for문으로 풀고 만약 common prefix가 ""이면 바로 반환 최초 comm..

코딩테스트 2024.02.14

136. Single Number

https://leetcode.com/problems/single-number/description/ LeetCode - The World'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 최초 풀이는 HashMap을 이용해서 배열의 요소의 개수를 체크한 후 개수가 1인 요소를 반환했다. 하지만 이렇게 풀이할 경우 공간 복잡도가 O(n)이었다. 문제에서는 공간복잡도 O(1)로 풀길 권장하고 있었기 ..

코딩테스트 2024.02.07

876. Middle of the Linked List

https://leetcode.com/problems/middle-of-the-linked-list/description/ LeetCode - The World'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 1. 전체값을 찾는다. 2. 전체값을 통해 중간값을 찾는다. 3. 중간값에 해당하는 노드는 재귀를 통해 찾는다. /** * Definition for singly-linked list. * ..

코딩테스트 2024.02.05

141. Linked List Cycle

https://leetcode.com/problems/linked-list-cycle/description/ Set에 ListNode를 저장해서 count 개수와 set의 size가 다르면 사이클이 있다고 판단하였다. /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ /** 1. 문제 해석하기 head가 주어지는데 이 링크드리스트에 사이클 존재 여부를 반환한다. 노드의 개수는 0부터 10000개까지 2. 문제 풀이 방향 모든 노드를 Set에 저장하면서 노드를 추가했는데도 Set의 개수가 변하지 않으..

코딩테스트 2024.01.29

21. Merge Two Sorted Lists

https://jaime-note.tistory.com/262 머리가 나빠서 node와 node를 연결하지 못하고 있다. 풀이 중 /** * 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; } * } */ /* 1. 문제 해석하기 정렬된 링크드 리스트가 list1, list2가 주어지고 이 리스트를 합쳐서 정렬된 형태의 리스트로 반환해줍니다. 2. 문제 풀이 방향 list..

코딩테스트 2024.01.29

235. Lowest Common Ancestor of a Binary Search Tree

링크 참고한 코드 풀이 코드 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } * * 1. 문제 해석하기 * 이진 탐색 트리의 가장 낮은 공통 조상을 찾아달라는 문제입니다. * 2. 문제 풀이 방법 * TreeNode를 따라 내려간다. * left의 val 혹은 right의 val이 p혹은 q일 경우 그 다음 값도 확인한다. * 조상이 p 혹은 q 인 경우도 있기 때문에 해당 경우도 고려한다. * 같은 숫자를 가진 노드는 없다. * * p의 조상 목록과 q의 조상 목록을 찾는다. (가장 처음은..

코딩테스트 2024.01.28