[JavaScript] leetCode : Single Number
๐Ÿ““ Algorithm/LeetCode 2022. 5. 26. 16:09

๋ฌธ์ œ Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. You must implement a solution with a linear runtime complexity and use only constant extra space. ์ •์ˆ˜์˜ ๋ฐฐ์—ด์—์„œ ๋™์ผํ•˜๊ฒŒ ์ง์ง€์–ด์ง€์ง€ ์•Š๋Š” ํ•˜๋‚˜์˜ ์ˆ˜๋ฅผ ์ฐพ์•„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ. ํ’€์ด let result = nums[0]; for (let i = 1; i < nums.length; i++) { result = result ^ nums[i]; } return result; ๊ฐ„๋‹จํ•ด๋ณด์ด๋ฉด์„œ๋„ ๋ฌ˜ํ•˜๊ฒŒ ๊ฐ„๋‹จํ•˜์ง€ ์•Š์•„์„œ ๊ฒฐ๊ตญ ๋‹ค๋ฅธ ์ฝ”๋“œ๋ฅผ ๋ณด๊ฒŒ ๋˜์—ˆ๋‹ค..

[JavaScript] leetCode : Reverse Words in a String
๐Ÿ““ Algorithm/LeetCode 2022. 5. 16. 20:25

๋ฌธ์ œ Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space. Return a string of the words in reverse order concatenated by a single space. Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separa..

[JavaScript] leetCode : Implement strStr()
๐Ÿ““ Algorithm/LeetCode 2022. 5. 12. 22:53

๋ฌธ์ œ Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. ๋‘๊ฐœ์˜ ๋ฌธ์ž์—ด needle, haystack์ด ์ฃผ์–ด์งˆ ๋•Œ, haystack์—์„œ needle์˜ ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , needle์ด haystack์˜ ํ•œ ๋ถ€๋ถ„์ด ์•„๋‹ˆ๋ผ๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํ’€์ด const strStr = function(haystack, needle) { if (needle.length === 0) return 0; let index = -1; if (haystack.includes(needle)) { let firstLette..

[JavaScript] leetCode : Longest Common Prefix
๐Ÿ““ Algorithm/LeetCode 2022. 5. 9. 23:16

๋ฌธ์ œ Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". ํ•œ ๋‹จ์–ด๊ฐ€ ์š”์†Œ์ธ ๋ฐฐ์—ด์ด ์ฃผ์–ด์งˆ ๋•Œ, ์š”์†Œ์˜ ๋ฌธ์ž์—ด์—์„œ ์กด์žฌํ•˜๋Š” ๊ฐ€์žฅ ๊ธด ์ ‘๋‘์‚ฌ๋ฅผ ์ฐพ์•„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์—†๋‹ค๋ฉด ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜. ํ’€์ด const longestCommonPrefix = (strs) => { if (strs.length === 0) return ''; if (strs.length === 1) return strs[0]; let prefix = ''; let prev = ''; for (let i = 0; i < strs[0].lengt..

[JavaScript] leetCode : Linked List Cycle II
๐Ÿ““ Algorithm/LeetCode 2022. 5. 3. 23:56

๋ฌธ์ œ Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to (0-indexed). It is -1 if there is no cycle. Note tha..

[JavaScript] leetCode : Valid Mountain Array
๐Ÿ““ Algorithm/LeetCode 2022. 5. 2. 21:57

๋ฌธ์ œ Given an array of integers arr, return true if and only if it is a valid mountain array. Recall that arr is a mountain array if and only if arr.length >= 3 There exists some i with 0 arr[i + 1] > ... > arr[arr.length - 1] ๋ฐฐ์—ด์ด ์‚ฐ์ฒ˜๋Ÿผ ์˜ค๋ฆ„์ฐจ์ˆœ-๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์—ฌ boolean์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ. ์กฐ๊ฑด์€ ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ 3 ์ด์ƒ์ด์–ด์•ผ ํ•˜๊ณ , ์˜ค๋ฆ„์ฐจ์ˆœ๊ณผ ๋‚ด๋ฆผ์ฐจ์ˆœ์˜ ..

[JavaScript] LeetCode : Add Binary
๐Ÿ““ Algorithm/LeetCode 2022. 4. 22. 17:25

๋ฌธ์ œ Given two binary strings a and b, return their sum as a binary string. 2์ง„์ˆ˜ ๋ฌธ์ž์—ด a,b๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ์ด๋“ค์„ ๋”ํ•œ ๊ฐ’์„ ๋‹ค์‹œ 2์ง„์ˆ˜ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ํ’€์ด let addBinary = function (a, b) { let sum = Bigint('0b' a) + BigInt('0b', 2); let result = sum.toString(2); return result; }; ์ฒ˜์Œ์— ์ƒ๊ฐํ•œ ๊ฒƒ์€ ๋ฌธ์ž์—ด์ธ a์™€ b๋ฅผ ์ •์ˆ˜ํ™” ํ•˜์—ฌ ๋”ํ•˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด parseInt ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ–ˆ์—ˆ๋‹ค. let sum = parseInt(a, 2) + parseInt(b, 2); ๋ณดํ†ต parseInt์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฌธ์ž์—ด๋งŒ ๋„ฃ์œผ๋ฉด ๋ฌธ์ž์—ด์„ ๋ฐ”..

[JavaScript] LeetCode : Plus One
๐Ÿ““ Algorithm/LeetCode 2022. 4. 21. 21:00

๋ฌธ์ œ You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's. Increment the large integer by one and return the resulting array of digits. ์ •์ˆ˜์˜ ๋ฐฐ์—ด๋กœ ํ‘œ์‹œ๋˜๋Š” ํฐ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ์š”์†Œ๋Š” ์ •์ˆ˜์˜ ์ž๋ฆฟ์ˆ˜์ด๋‹ค. ์ˆซ์ž๋Š” ์™ผ์ชฝ->์˜ค๋ฅธ..

[JavaScript] LeetCode : Largest Number At Least Twice of Others
๐Ÿ““ Algorithm/LeetCode 2022. 4. 20. 22:10

๋ฌธ์ œ You are given an integer array nums where the largest integer is unique. Determine whether the largest element in the array is at least twice as much as every other number in the array. If it is, return the index of the largest element, or return -1 otherwise. ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜์˜ ๋ฐฐ์—ด์—์„œ ๊ฐ€์žฅ ํฐ ์ˆ˜๊ฐ€ ๋ฐฐ์—ด ๋‚ด ๋‹ค๋ฅธ ์ˆ˜์˜ ์ตœ์†Œ ๋‘ ๋ฐฐ ์ด์ƒ์ธ์ง€ ํ™•์ธํ•˜๊ณ , ์กฐ๊ฑด์— ์ถฉ์กฑ๋œ๋‹ค๋ฉด ๊ฐ€์žฅ ํฐ ์ˆ˜์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํ’€์ด var dominantIndex = function..

[JavaScript] LeetCode : Find Pivot Index
๐Ÿ““ Algorithm/LeetCode 2022. 4. 20. 21:46

๋ฌธ์ œ Given an array of integers nums, calculate the pivot index of this array. The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right. If the index is on the left edge of the array, then the left sum is 0 because there are no elements to the left. This also applies to the right edge of the..