본문 바로가기

JAVA/Coding Test Study151

[Easy] LeetCode - no.21 Merge Two Sorted Lists : Java https://leetcode.com/problems/merge-two-sorted-lists/description/?envType=study-plan-v2&envId=top-interview-150\  문제두 개의 정렬된 연결 리스트가 주어졌을 때, 이를 정렬된 순서(오름차순) 를 지키며 병합시킬 것 Example 1   Input: list1 = [1,2,4], list2 = [1,3,4]Output: [1,1,2,3,4,4]     풀이1 public static ListNode mergeTwoLists(ListNode list1, ListNode list2) { if (list1 != null && list2 != null) { if (list1.val  참고.. 2024. 10. 21.
[Easy] LeetCode - no.141 Linked List Cycle : Java https://leetcode.com/problems/linked-list-cycle/description/?envType=study-plan-v2&envId=top-interview-150  문제주어진 LinkedList에 사이클이 존재하는지 여부를 판단할 것 Example 1Input: head = [3,2,0,-4], pos = 1Output: trueExplanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).   (이 때, 파라미터로는 시작 노드만 주어짐)   풀이1 public boolean hasCycle(ListNode head) { Set set = .. 2024. 10. 21.
[Easy] LeetCode - no.20 Valid Parentheses : Java https://leetcode.com/problems/valid-parentheses/description/?envType=study-plan-v2&envId=top-interview-150  문제(, ), {, }, [, ] 로 이루어진 String s가 유효한지 판단할 것이 때, 유효하다는 것은 열리는 괄호와 닫히는 괄호가 서로 매칭되는지 여부Example 2:Input: s = "()[]{}"Output: trueExample 3:Input: s = "(]"Output: false   풀이1 public static boolean isValid(String s) { Stack stack = new Stack(); for (char c : s.toCharArray()) .. 2024. 10. 19.
[Easy] LeetCode - no.228 Summary Ranges : Java https://leetcode.com/problems/summary-ranges/description/?envType=study-plan-v2&envId=top-interview-150  문제int 배열 nums가 주어졌을 때, 연속되는 숫자의 범위들을 String 리스트로 반환할 것Example 1:Input: nums = [0,1,2,4,5,7] Output: ["0->2","4->5","7"] Explanation: The ranges are: [0,2] --> "0->2" [4,5] --> "4->5" [7,7] --> "7"Example 2:Input: nums = [0,2,3,4,6,8,9] Output: ["0","2->4","6","8->9"] Explanation: The ranges a.. 2024. 10. 19.
[Easy] LeetCode - no.219 Contains Duplicate II : Java https://leetcode.com/problems/contains-duplicate-ii/description/?envType=study-plan-v2&envId=top-interview-150  문제int 배열 nums가 주어졌을 때, 똑같은 숫자끼리의 인덱스 차이가 k 이하인지 여부 판단Example 1:Input: nums = [1,2,3,1], k = 3 Output: true Example 2:Input: nums = [1,0,1,1], k = 1 Output: true Example 3:Input: nums = [1,2,3,1,2,3], k = 2 Output: false   풀이1 public static boolean containsNearbyDuplicate(int[] nums,.. 2024. 10. 17.
[Easy] LeetCode - no.202 Happy Number : Java https://leetcode.com/problems/happy-number/description/?envType=study-plan-v2&envId=top-interview-150  문제숫자 n이 happy number인지 여부 판단happy number란 각 자리 숫자들을 각각 제곱한 값을 계속 더해가다가 결국 1이 되는 숫자를 말함Example 1:Input: n = 19 Output: true Explanation: 1^2 + 9^2 = 82 8^2 + 2^2 = 68 6^2 + 8^2 = 100 1^2 + 0^2 + 0^2 = 1 Example 2:Input: n = 2 Output: false   풀이1 public static boolean isHappy(int n) { i.. 2024. 10. 17.