[LeetCode] 219. Contains Duplicate II, Easy
·
CodingTest/LeetCode
1. 문제정수 배열 nums와 정수 k가 주어질 때, 두 요소가 같고 인덱스 차이의 절대값이 k 이하인 경우 true, 아니면 false를 반환하라.2. 해결function containsNearbyDuplicate(nums: number[], k: number): boolean { const map = new Map(); for(let i = 0; i(); // for(let i = 0; i 1) { // let p1 =0, p2 = 1; // while(p1 처음에는 같은 숫자 별로 인덱스를 배열에 저장하고, 해당 배열의 모든 경우의 수에서 구할 수 있는 절대값으로 k를 비교했다.그럴 필요 없이 최신 인덱스만 map에..
[LeetCode] 350. Intersection of Two Arrays II, Easy
·
CodingTest/LeetCode
1. 문제정수 배열 nums1, nums2가 주어질 때, 두 배열에서 공통으로 등장하는 요소를 반환하라.이 결과에는 등장 횟수만큼 중복될 수 있다. (349번 문제는 고유한 요소 반환)2. 해결function intersect(nums1: number[], nums2: number[]): number[] { const map = new Map(); for(const num of nums1) { if(map.has(num)) map.set(num, map.get(num) + 1) else map.set(num, 1) } const result = []; for(const num of nums2) { if(map.has(num) ..
[LeetCode] 387. First Unique Character in a String, Easy
·
CodingTest/LeetCode
1. 문제문자열 s가 주어질 때, 반복되지 않는 문자 중 가장 먼저 등장하는 문자의 인덱스를 반환하라.없으면 -1을 반환하라.2. 해결function firstUniqChar(s: string): number { const map = new Map(); let index = 0; for(const char of s) { if(!map.has(char)) map.set(char, {idx: index, count:1}); else { const {idx, count} = map.get(char); map.set(char, {idx, count: count + 1}); } i..
[LeetCode] 599. Minimum Index Sum of Two Lists, Easy
·
CodingTest/LeetCode
1. 문제문자열 list1, list2가 주어질 때, 공통으로 존재하는 문자열의 두 인덱스 차이가 가장 작은 문자열을 담아 반환하라.2. 해결function findRestaurant(list1: string[], list2: string[]): string[] { const map1 = new Map(); const map2 = new Map(); for(let i = 0; i 두 문자 배열을 탐색하며 Map에 문자열-인덱스로 맵핑.한쪽 Map을 탐색하며, 다른 쪽 Map에 있으면서 최소값 갱신.최소값보다 작은 경우가 나오면 result 배열을 초기화 후 담기.
[LeetCode] 205. Isomorphic Strings, Easy
·
CodingTest/LeetCode
1. 문제문자열 s와 t가 주어질 때, 순서를 유지하면서 맵핑되어 문자열 s를 t로 만들 수 있는 지 반환하라.2. 해결function isIsomorphic(s: string, t: string): boolean { const sArr = Array.from(s); const tArr = Array.from(t); const sMap = new Map(); const tMap = new Map(); for(let i =0; i문제 자체를 이해하기 힘들었다.결국, 문자열 s에 각 문자와 동일한 위치에 있는 t의 문자가 1:1로 맵핑되는 지 확인하면 된다.s: badc t: baba로 주어질 때,b - ba - a는 맵핑이 되지만, d - b는 이미 앞에서 b-b로 맵핑되..
[LeetCode] 1. Two Sum, Easy
·
CodingTest/LeetCode
1. 문제정수 배열 nums와 정수 target이 주어질 때, 요소 2개의 합이 target이 되는 두 인덱스 배열을 반환하라.오직 1개만 만족하는 쌍이 존재한다.2. 해결function twoSum(nums: number[], target: number): number[] { const map = new Map(); for(let i = 0; i 탐색하면서 target에서 뺀 값을 맵핑하기 위한 map을 구성.target에서 뺀 값이 map에 존재하면, 해당 인덱스 정보를 활용해 0이 되는지 확인.
[LeetCode] 202. Happy Number, Easy
·
CodingTest/LeetCode
1. 문제정수 n이 주어질 때 해당 정수가 happy number인지 판단하라.각 자리수의 제곱 후 더한다.더한 결과가 1이 될 때 까지 반복한다.무한 반복되면 해당 수는 happy number가 아니다.2. 해결function isHappy(n: number): boolean { const set = new Set(); let sum:number = n; while(sum !== 1) { let newSum = 0; for(const num of String(sum)) { newSum += Math.pow(Number(num),2); } if(set.has(newSum)) return fa..
[LeetCode] 349. Intersection of Two Arrays, Easy
·
CodingTest/LeetCode
1. 문제정수 배열 nums1, nums2가 주어질 때, 두 배열이 공통적으로 가지는 요소를 담은 배열을 반환하라.2. 해결function intersection(nums1: number[], nums2: number[]): number[] { const set = new Set(nums1); for(const num of set) { if(!nums2.includes(num)) set.delete(num); } return Array.from(set);};한 배열을 Set으로 담아 중복을 없애주고, 탐색하면서 다른 배열에 포함되지 않은 값은 삭제해준다.
[LeetCode] 136. Single Number, Easy
·
CodingTest/LeetCode
1. 문제정수 배열 nums가 주어질 때, 하나의 정수는 오직 1개 들어있으며 그 외는 2개 씩 들어 있다고 할때 1개만 들어있는 정수를 반환하라.2. 해결function singleNumber(nums: number[]): number { const set = new Set(); for(let i = 0; iSet을 이용해서 특정 요소가 없으면 넣어주고 있으면 삭제한다.1번 등장하는 정수를 제외하고 나머지는 2번 씩 등장하므로 추가 -> 삭제 될 수 밖에 없다.Set에 남은 1개가 정답.
[LeetCode] 217. Contains Duplicate, Easy
·
CodingTest/LeetCode
1. 문제정수 배열 nums가 주어질 때, 동일한 값이 2번 이상 나타나면 true를 반환하고, 그렇지 않으면 false를 반환하라.2. 해결function containsDuplicate(nums: number[]): boolean { const set = new Set(); for(let i=0; i중복 여부를 판단할 때 Set을 사용하면 간편하다.