[LeetCode] 94. Binary Tree Inorder Traversal, Easy
·
CodingTest/LeetCode
1. 문제이진트리가 주어질 때, 중위 순회 결과를 반환하라.2. 해결/** * Definition for a binary tree node. * class TreeNode { * val: number * left: TreeNode | null * right: TreeNode | null * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefi..
[LeetCode] 20. Valid Parentheses, Easy
·
CodingTest/LeetCode
1. 문제(){}[] 각 문자로 구성된 문자열이 주어질 때, 옳바른 괄호로 구성되어 있는지 반환하라.2. 해결function isValid(s: string): boolean { const stack = new Array(); const map = { ')' : '(', ']' : '[', '}' : '{' } for(const char of s) { if(!map.hasOwnProperty(char)) stack.push(char); else { const top = stack.pop(); if(top !== map[char]) return false; } ..
[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] 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..
[LeetCode] 206. Reverse Linked List, Easy
·
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) * } * } */function reverseList(head: ListNode | null): ListNode | null { if(!head..
[LeetCode] 160. Intersection of Two Linked Lists, Easy
·
CodingTest/LeetCode
1. 문제단일 연결 리스트 headA, headB가 주어질 때, 두 연결 리스트의 요소 중 동일한 주소를 가지는 첫 번째 노드를 반환하라.만약, 없다면 null을 반환하라.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 getInt..
[LeetCode] 141. Linked List Cycle, Easy
·
CodingTest/LeetCode
1. 문제연결 리스트의 head가 주어질 때, 해당 리스트 내 사이클이 존재하는 지 true 또는 false를 반환하라.pos는 전달되지 않는 값이다.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 hasCycle(head: Lis..
[LeetCode] 557. Reverse Words in a String III, Easy
·
CodingTest/LeetCode
1. 문제문자열 s가 주어질 때, 공백으로 구분되는 각 단어를 뒤집어서 반환하라.2. 풀이function reverseWords(s: string): string { return s.split(' ').map(word => word.split('').reverse().join('')).join(' ')};공백으로 각 단어를 구분하는 배열을 만들고, 각 단어를 다시 문자 단위로 쪼갠 배열을 뒤집어 연결하고 공백을 기준으로 join 해주면 된다.투 포인터를 사용할 수도 있는데 코드가 복잡해질거 같다.
[LeetCode] 119. Pascal's Triangle II, Easy
·
CodingTest/LeetCode
1. 문제위 그림처럼 만들어지는 pascal 삼각형이 있을 때, 주어진 rowIndex에 해당하는 row 배열을 반환하라.유사 문제 https://fbtmdwhd33.tistory.com/319 [LeetCode] 118. Pascal's Triangle, Easy1. 문제정수 numRows가 주어질 때, 그림과 같은 파스칼 트라이앵글을 만들어 2차원 배열로 반환하라.여기서, 파스칼 트라이앵글은 문제에 들어가면 애니메이션으로 보여주지만, 위 2개의 수를 합한fbtmdwhd33.tistory.com2. 해결function getRow(rowIndex: number): number[] { const result = [[1]]; if(rowIndex === 0) return result[0]..