easy8 [Leetcode][자바스크립트][Easy] 141. Linked List Cycle 풀이1 /** * 141_LinkedListCycle.js * Easy * https://leetcode.com/problems/linked-list-cycle/ */ const nodeArr = []; var hasCycle = function(head) { if (head === null || head.next === null) return false; if (nodeArr.indexOf(head.next) > -1) return true; nodeArr.push(head.next); return hasCycle(head.next); }; 문제 예시에서 input 이 배열로 주어지는 것처럼 나타나서 처음에 좀 헷갈렸다. hasCycle 함수 자체를 재귀로 놓고 돌렸는데 그 결과로 nodeArr 배열이.. 2022. 1. 11. [Leetcode][자바스크립트][Easy] 242. Valid Anagram 풀이 /** * 242_ValidAnagram.js * Easy * https://leetcode.com/problems/valid-anagram/ */ var isAnagram = function(s, t) { if (s.length !== t.length) return false; const sSorted = s.split("").sort().join(""); const tSorted = t.split("").sort().join(""); return sSorted === tSorted; }; anagram 을 구하는 문제로 주어진 두 string의 모든 char 의 종류와 개수가 동일해야한다. 결국 동일한 방식으로 정렬했을 때 동일한 값이어야한다. 2022. 1. 10. [Leetcode][자바스크립트][Easy] 383. Ransom Note 풀이 /** * 383_RansomNote.js * Easy * https://leetcode.com/problems/ransom-note/ */ var canConstruct = function(ransomNote, magazine) { const mHash = {}; magazine.split("").forEach((char) => { if (!mHash[char]) { mHash[char] = 1; } else { mHash[char]++; } }); for (let i = 0; i < ransomNote.length; i++) { if (!mHash[ransomNote[i]]) return false; mHash[ransomNote[i]]--; } return true; }; ransomNote .. 2022. 1. 10. [Leetcode][자바스크립트][Easy] 387. First Unique Character in a String 풀이 /** * 387_FirstUniqueCharacterInAString.js * Easy * https://leetcode.com/problems/first-unique-character-in-a-string/ */ var firstUniqChar = function(s) { const strHash = {}; const sArr = s.split(""); sArr.forEach((char) => { if (!strHash[char]) { strHash[char] = 1; } else { strHash[char]++; } }); for (let i = 0; i < sArr.length; i++) { if (strHash[sArr[i]] === 1) return i; } return -1; }; 해시.. 2022. 1. 10. [Leetcode][자바스크립트][Easy] 566. Reshape the Matrix 풀이1 /** * 566_ReshapeTheMatrix.js * Easy * https://leetcode.com/problems/reshape-the-matrix/ */ var matrixReshape = function(mat, r, c) { if (mat[0].length * mat.length !== r * c) return mat; let temp = []; mat.forEach((subArr) => temp = temp.concat(subArr)); const answer = [[]]; temp.forEach((val) => { if (answer[answer.length - 1].length === c) answer.push([]); answer[answer.length - 1].push(v.. 2022. 1. 9. [Leetcode][자바스크립트][Easy] 121. Best Time to Buy and Sell Stock 풀이 /** * 121_BestTimeToBuyAndSellStock.js * Easy * https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ */ var maxProfit = function(prices) { let currMin = prices[0], currMax = 0, _maxProfit = 0; prices.forEach((price) => { if (price > currMax) { currMax = price; _maxProfit = Math.max(_maxProfit, currMax - currMin); } if (price currMin) _maxProfit = Math.max(.. 2022. 1. 8. [Leetcode][자바스크립트][Easy] 350. Intersection of Two Arrays II 풀이1 /** * 350_IntersectionOfTwoArraysII.js * Easy * https://leetcode.com/problems/intersection-of-two-arrays-ii/ */ var intersect = function(nums1, nums2) { const answer = [], idxHash = {}; nums1.forEach((num) => { const idx = idxHash[num] === undefined ? nums2.indexOf(num) : nums2.indexOf(num, idxHash[num] + 1); if (idx > -1) { idxHash[num] = idx; answer.push(num); } }); return answer; }; 교집합을 .. 2022. 1. 8. [Leetcode][자바스크립트][Easy] 88. Merge Sorted Array /** * 88_MergeSortedArray.js * Easy * https://leetcode.com/problems/merge-sorted-array/ * @param {number[]} nums1 * @param {number} m * @param {number[]} nums2 * @param {number} n * @return {void} Do not return anything, modify nums1 in-place instead. */ // built-in 사용 var merge1 = function(nums1, m, nums2, n) { nums1.splice(m); nums1.push(...nums2); nums1.sort((a, b) => a - b); // console.log({.. 2022. 1. 6. 이전 1 다음