[LeetCode] 383. Ransom Note, Easy
·
CodingTest/LeetCode
1. 문제ransomeNote, magazine 두 문자열이 주어질 때, magazine의 각 문자로 ransomeNote 문자열을 만들 수 있으면 true, 아니면 false를 반환.magazine의 각 문자는 1 번만 쓰일 수 있다.두 문자열에는 오로지 알파벳 소문자로 제한된다.2. 해결function canConstruct(ransomNote: string, magazine: string): boolean { // 1. a~z의 아스키코드 범위는 97 ~ 122이므로, 97을 뺀 0 ~ 25 인덱스 배열 생성. const countingArr = new Array(26).fill(0) // 2. magazine 각 문자의 아스키코드 계산 후 해당 인덱스 값 증가. for(let i..
[LeetCode] 876. Middle of the Linked List, Easy
·
CodingTest/LeetCode
1. 문제단일 연결 리스트 head가 주어질 때, 연결 리스트의 중간 노드를 반환하라.만약, 중간 노드가 2개(전체 길이가 짝수인 경우) 2번 째 노드를 반환하라.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 middleNode(..
[LeetCode] 1342. Number of Steps to Reduce a Number to Zero, Easy
·
CodingTest/LeetCode
1. 문제정수 num이 주어질 때, num이 짝수면 2로 나누고 홀수면 1을 빼준다.이 과정을 0이 될 때 까지 반복해서 몇 번 수행하는지 결과값을 반환한다.2. 해결function numberOfSteps(num: number): number { let step = 0; while(num > 0) { if(num % 2 === 0) { num /=2; } else { num -=1; } step++; } return step;};그냥 반복 돌면서 조건에 맞게 연산 수행 후, step을 증가시키면 된다.
[LeetCode] 412. Fizz Buzz, Easy
·
CodingTest/LeetCode
1. 문제정수 n이 주어질 때, 크기 n의 배열의 요소로 Fizz, Buzz, FizzBuzz, 문자열 index를 저장 후 반환.index는 1부터 시작하고, index를 기준으로 3으로 나누어 떨어지면 Fizz, 5로 나누어 떨어지면 Buzz, 둘 다 나누어 떨어지면 FizzBuzz, 그 외는 index를 문자열로 저장.ex. 1은 문자열로 변경 후 저장은 i-1에 저장2. 해결/**1. 3과 5로 나뉘어 지면 FizzBuzz.2. 3으로 나뉘어 지면 Fizz.3. 5로 나뉘어 지면 Buzz.4. 해당 안되면 문자열 index.index는 1부터 시작. */function fizzBuzz(n: number): string[] { const fizzBuzz : string[] = new Array..
[LeetCode] 1672. Richest Customer Wealth, Easy
·
CodingTest/LeetCode
1. 문제행을 고객, 열을 은행이라 가정하고, 각 고객마다 은행에 있는 계좌의 수(?) 혹은 돈의 가장 큰 값을 반환해라.2. 해결function maximumWealth(accounts: number[][]): number { let max = 0; // 1. 고객 반복문 accounts.forEach((customer, index) => { // let wealth = 0; // accounts[index].forEach((bank, bIndex) => { // wealth += accounts[index][bIndex]; // }) // 2. 은행 반복 및 누산기. const wealth = acco..
[LeetCode] 1480. Running Sum of 1d Array, Easy
·
CodingTest/LeetCode
1. 문제sums 배열이 주어질 때, runnigSum[i] = sum[0] + sum[1] + ... + sum[i] 값으로 구성된 runningSum 배열을 만들어라.2. 해결function runningSum(nums: number[]): number[] { const memoizedArray = new Array(nums.length).fill(0); // 1. memoized array. // 2. 값 계속 누적해서 다시 계산할 필요 없게. nums.forEach((num, index) => { if (index === 0) memoizedArray[index] = num; else memoizedArray[ind..
[LeetCode] 226. Invert Binary Tree, Easy
·
CodingTest/LeetCode
1. 문제 이진 트리가 주어질 때, 각 자식 노드들을 좌우반전 시켜서 root node를 반환하라.완전 이진 트리가 아님에 주의, 자식 노드가 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===..
[LeetCode] 2236. Root Equals Sum of Children, Easy
·
CodingTest/LeetCode
1. 문제 정확히 3개의 node로 구성된 이진 트리가 주어질 때, 왼쪽 자식과 오른쪽 자식의 합이 root의 값과 동일하면 true, 아니면 false를 반환.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===undefi..
[LeetCode] 2235. Add Two Integers, Easy
·
CodingTest/LeetCode
1. 문제 num1, num2가 주어질 때, 두 정수의 합을 리턴하는 함수를 작성.2. 해결function sum(num1: number, num2: number): number { return num1 + num2};단순 수학 문제.이직 준비를 위해 슬슬 풀어보자~