[LeetCode] 200. Number of Islands, Medium
·
CodingTest/LeetCode
1. 문제m x n 크기의 2차원 배열이 주어지고, 각 요소는 1(섬), 0(바다)로 구성되어 있을 때, 존재하는 섬의 개수를 반환하라.섬은 수직 또는 수평으로 다른 인접한 섬을 가지고 있고, 바다로 둘러쌓여 있다. (모서리는 바다로 가정)2. 해결function numIslands(grid: string[][]): number { const queue = new Array(); const row = [-1, 1, 0, 0]; const col = [0, 0, -1, 1]; let count = 0; for(let i=0; i 0) { const [x,y] =queue.shift(); for(let k=0; k..
[LeetCode] 622. Design Circular Queue, Medium
·
CodingTest/LeetCode
1. 문제요구사항에 맞는 Circular Queue를 구현하라2. 해결class MyCircularQueue { queue: Array; front: number; rear: number; constructor(k: number) { this.queue = new Array(k+1).fill(null); this.front = 0; this.rear = 0; } enQueue(value: number): boolean { if(this.isFull()) return false; this.queue[this.rear] = value; this.rear = (this..
[LeetCode] 61. Rotate List, Medium
·
CodingTest/LeetCode
1. 문제단일 연결리스트와 정수 k가 주어질 때, k만큼 오른쪽으로 회전한 리스트를 반환하라.2. 해결/** * Definition for singly-linked list. * class ListNode { * val: number * next: ListNode | null * constructor(val?: number, next?: ListNode | null) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } * } */function rotateRight(head: ListNode | null, k: number): Lis..
[LeetCode] 138. Copy List with Random Pointer, Medium
·
CodingTest/LeetCode
1. 문제next와 random 필드를 가지는 노드로 구성된 단일 연결 리스트를 깊은 복사를 통해 새로운 노드로 구성된 리스트를 반환하라.새로운 리스트는 기존 노드를 참조해서는 안되고, 기존 리스트 역시 변경되면 안된다./** * Definition for _Node. * class _Node { * val: number * next: _Node | null * random: _Node | null * * constructor(val?: number, next?: _Node, random?: _Node) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? nul..
[LeetCode] 430. Flatten a Multilevel Doubly Linked List, Medium
·
CodingTest/LeetCode
child라는 필드를 추가적으로 가지는 멀티 레벨 이중 연결 리스트가 주어질 때, 모든 child를 없애 단일 이중 연결 리스트로 재구성 하라.2. 해결/** * Definition for _Node. * class _Node { * val: number * prev: _Node | null * next: _Node | null * child: _Node | null * * constructor(val?: number, prev? : _Node, next? : _Node, child? : _Node) { * this.val = (val===undefined ? 0 : val); * this.prev = (prev===undefine..
[LeetCode] 2. Add Two Numbers, Medium
·
CodingTest/LeetCode
1. 문제2개의 단일 연결 리스트가 주어질 때, 같은 인덱스를 가지는 각 노드를 합하여 1개의 단일 연결 리스트를 반환하라.2. 해결/** * Definition for singly-linked list. * class ListNode { * val: number * next: ListNode | null * constructor(val?: number, next?: ListNode | null) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } * } */function addTwoNumbers(l1: ListNode | null..
[LeetCode] 21. Merge Two Sorted Lists, Easy
·
CodingTest/LeetCode
1. 문제두 단일 연결 리스트가 정렬된 채로 주어질 때, 오름차순으로 정렬된 1개의 단일 연결리스트를 반환하라./** * Definition for singly-linked list. * class ListNode { * val: number * next: ListNode | null * constructor(val?: number, next?: ListNode | null) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } * } */function mergeTwoLists(list1: ListNode | null, list2: ..
[LeetCode] 234. Palindrome Linked List, Easy
·
CodingTest/LeetCode
1. 문제단일 연결 리스트가 주어질 때, 팰린드롬 리스트 여부를 반환하라.팰린드롬 또는 회문 : 앞 뒤가 동일하게 구성되어 있는 것. madam, 12321 등등2. 해결/** * Definition for singly-linked list. * class ListNode { * val: number * next: ListNode | null * constructor(val?: number, next?: ListNode | null) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } * } */function isPalindrome..
[LeetCode] 328. Odd Even Linked List, Medium
·
CodingTest/LeetCode
1. 문제단일 연결 리스트가 주어질 때, 값과 상관 없이 첫 번째 인덱스는 홀수, 두 번째 인덱스는 짝수와 같이 쭉 반복되는 형태일 때 홀수 그룹 리스트 + 짝수 그룹 리스트를 연결한 리스트를 반환하라.2. 해결/** * Definition for singly-linked list. * class ListNode { * val: number * next: ListNode | null * constructor(val?: number, next?: ListNode | null) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } * } ..
[LeetCode] 203. Remove Linked List Elements, Easy
·
CodingTest/LeetCode
1. 문제단일 연결 리스트와 val가 주어질 때, val와 일치하는 노드를 제거한 리스트를 반환하라.2. 해결/** * Definition for singly-linked list. * class ListNode { * val: number * next: ListNode | null * constructor(val?: number, next?: ListNode | null) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } * } */function removeElements(head: ListNode | null, val: num..