๋ฌธ์ 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; ๊ฐ๋จํด๋ณด์ด๋ฉด์๋ ๋ฌํ๊ฒ ๊ฐ๋จํ์ง ์์์ ๊ฒฐ๊ตญ ๋ค๋ฅธ ์ฝ๋๋ฅผ ๋ณด๊ฒ ๋์๋ค..
๋ฌธ์ 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..
๋ฌธ์ 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..
๋ฌธ์ 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..
๋ฌธ์ 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..
๋ฌธ์ 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 ์ด์์ด์ด์ผ ํ๊ณ , ์ค๋ฆ์ฐจ์๊ณผ ๋ด๋ฆผ์ฐจ์์ ..
๋ฌธ์ 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์ ๋งค๊ฐ๋ณ์๋ก ๋ฌธ์์ด๋ง ๋ฃ์ผ๋ฉด ๋ฌธ์์ด์ ๋ฐ..
๋ฌธ์ 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. ์ ์์ ๋ฐฐ์ด๋ก ํ์๋๋ ํฐ ์ ์๊ฐ ์ฃผ์ด์ง๋ค. ๊ฐ ์์๋ ์ ์์ ์๋ฆฟ์์ด๋ค. ์ซ์๋ ์ผ์ชฝ->์ค๋ฅธ..
๋ฌธ์ 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..
๋ฌธ์ 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..
Comment