[LeetCode] 345. Reverse Vowels of a String, Easy
·
CodingTest/LeetCode
1. 문제문자열 s가 주어질 때, 모음의 순서를 뒤집은 문자열을 반환하라IceCreAm의 모음 순서는 I, e, e, A => A, e, e, I로 뒤집어 바꾼 뒤 반환.2. 해결function reverseVowels(s: string): string { const vowels = [97, 101, 105, 111, 117]; const strVowels = []; for(const char of s) { if (vowels.includes(char.toLowerCase().charCodeAt(0))) { strVowels.push(char) } } strVowels.reverse(); const sArr = s..
[LeetCode] 605. Can Place Flowers, Easy
·
CodingTest/LeetCode
1. 문제화단 배열 flowerbed가 0은 비어있는 곳, 1은 꽃이 심어져 있는 곳이라할 때 인접한 곳에는 꽃을 심을 수 없다는 규칙이 있다.이 때 n개의 꽃을 모두 심을 수 있으면 true, 아니면 false를 반환하라.2. 해결function canPlaceFlowers(flowerbed: number[], n: number): boolean { let pointer = 0; while(pointer 0) { if(flowerbed[pointer] === 1) { pointer++; continue; } let dx = [-1, 1]; if(pointer === 0) { dx =..
[LeetCode] 1431. Kids With the Greatest Number of Candies, Easy
·
CodingTest/LeetCode
1. 문제n명의 학생이 가지고 있는 사탕의 수를 나타내는 배열 candies와 남은 사탕 extraCandies가 주어질 때, 각 학생에게 extraCandies를 준 뒤 학생들 중 가장 많은 사탕의 수이면 true를 담고 아니면 false를 담아 반환하라.2. 해결function kidsWithCandies(candies: number[], extraCandies: number): boolean[] { let max = Number.MIN_SAFE_INTEGER; const ans = [] for(const candy of candies) max = Math.max(max, candy); for(const candy of candies) { if(candy + extr..
[LeetCode] 1071. Greatest Common Divisor of Strings, Easy
·
CodingTest/LeetCode
1. 제목문자열 str1과 str2가 주어질 때, 두 문자열을 구성할 수 있는 최대 문자열 x를 구하라.2. 해결function gcdOfStrings(str1: string, str2: string): string { if(str1 + str2 !== str2 + str1) return ''; function gcd(a: number, b: number) { return b === 0 ? a : gcd(b, a % b); } return str1.slice(0, gcd(str1.length, str2.length)) // let isLong1 = str1.length > str2.length ? true : false; // let minLength ..
[LeetCode] 1768. Merge Strings Alternately
·
CodingTest/LeetCode
1. 문제두 문자열 word1, word2가 주어질 때, word1부터 한 글자씩 번갈아 가며 병합한 문자열을 반환하라.한 문자열이 끝난 경우, 나머지 문자열의 나머지 부분은 뒤에 붙인다.2. 해결function mergeAlternately(word1: string, word2: string): string { let p1 = 0, p2 = 0; let isWord1 = true; let ans = ''; while(p1 두 문자열이 끝에 도달하지 않은 경우, 반복하며 번갈아 문자열을 붙인다.둘 중 한 개라도 끝에 도달한 경우, pointer를 확인하고 나머지 문자열을 붙인다.
[LeetCode] 108. Convert Sorted Array to Binary Search Tree, Easy
·
CodingTest/LeetCode
1. 문제오름차순 정렬된 배열이 주어질 때, 균형잡인 BST 를 구성하라.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=..
[LeetCode] 110. Balanced Binary Tree, 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===undefine..
[LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree, Medium
·
CodingTest/LeetCode
1. 문제BST와 p, q노드가 주어질 때, p와 q의 공통 조상 노드를 반환하라.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 = (r..
[LeetCode] 703. Kth Largest Element in a Stream, Easy
·
CodingTest/LeetCode
1. 문제정수 배열과 k가 주어질 때, add 후 k 번째로 큰 숫자를 반환하는 클래스를 작성하라.2. 해결class TreeCountNode { cnt: number; val: number; left: TreeCountNode | null; right: TreeCountNode | null; constructor(val:number, left?: TreeCountNode | null, right?: TreeCountNode | null) { this.cnt = 1; this.val = val; this.left = left || null; this.right = right || null; }}class KthLargest..
[LeetCode] 450. Delete Node in a BST, Medium
·
CodingTest/LeetCode
1. 문제BST와 정수 key가 주어질 때, 해당 key를 제거한 BST를 반환하라.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 = (..