TIL(Today I Learned)/2021년
09.21 - LeetCode1_twoSum / Map & Object / Set & Array
심리밀당남
2021. 9. 21. 20:30
목차
1. LeetCode 1번 - twoSum
/* https://leetcode.com/problems/two-sum/
* solution을 참고한 알고리즘
* HashMap 자료구조 사용
** key = value of nums[], value = index of nums[]
* 어려운 알고리즘 사용할 필요 없구나... -> 애매한 알고리즘 지식은 생각을 가두는 틀로 작용할 수 있다
*/
const twoSum = function (nums, target) {
let map = new Map()
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
// map.has()로 이중 for문 대체
if (map.has(complement)) return [i, map.get(complement)].sort((a, b) => a - b)
map.set(nums[i], i);
}
}
2. Map & Object / Set & Array - 정리하기
https://developer.mozilla.org/ko/docs/Web/JavaScript/Guide/Keyed_collections
키기반의 컬렉션 - JavaScript | MDN
이번 장에서는 입력된 키값을 기준으로 정렬되는 데이터의 집합(자료 구조)에 대해 소개 할 것이다. Map과 Set은 입력된 순서대로 반복적으로 접근 가능한 요소들을 포함하고 있다.
developer.mozilla.org
https://kellis.tistory.com/129
[ES6+] Map vs Object
이 글에서는 ES6에서 새롭게 도입된 Map에 대해서 알아보고, 이것이 Object Literal과는 무슨 차이가 있는지 살펴보도록 하겠습니다. (1) Map? Map은 대부분 개발자에게 익숙한 단어일 것입니다. ECMA Script
kellis.tistory.com