JavaScript 15

[leetcode, JS] 67. Add Binary

문제 문제 링크 : Add Binary 풀이 /** * @param {string} a * @param {string} b * @return {string} */ var addBinary = function(a, b) { let binA = BigInt("0b"+a) let binB = BigInt("0b"+b) return (binA+binB).toString(2); }; a,b를 10진수의 BigInt값으로 바꾸고 두 값을 더한 후 2진수로 변환한 값을 return Runtime 62 ms, Memory 42.2 MB 다른 풀이 /** * @param {string} a * @param {string} b * @return {string} */ var addBinary = function(a, b) ..

[leetcode, JS] 58. Length of Last Word

문제 문제 링크 : Length of Last Word 풀이 /** * @param {string} s * @return {number} */ var lengthOfLastWord = function(s) { let arr = s.split(' '); let result = 0 for(let i=arr.length-1; i>-1; i--){ if(arr[i] !== '') { result = arr[i].length; break; } } return result; }; s를 split으로 배열로 만들고 배열의 끝에서부터 빈 값이 아닌 값의 length를 return Runtime 41 ms, Memory 41.7 MB 다른 풀이 /** * @param {string} s * @return {number}..

[leetcode, JS] 28. Find the Index of the First Occurrence in a String

문제 문제 링크 : Find the Index of the First Occurrence in a String 풀이 /** * @param {string} haystack * @param {string} needle * @return {number} */ var strStr = function(haystack, needle) { return haystack.includes(needle) ? haystack.indexOf(needle) : -1 }; haystack에 needle이 포함되어 있는지 확인하여 있으면 indexOf 값, 없다면 -1을 return Runtime 48 ms, Memory 41.4 MB

[leetcode, JS] 26. Remove Duplicates from Sorted Array

문제 문제 링크 : Remove Duplicates from Sorted Array 풀이 /** * @param {number[]} nums * @return {number} */ var removeDuplicates = function(nums) { const deduplicatedSet = new Set(nums); let deduplicatedArray = Array.from(deduplicatedSet); for (var i = 0; i < deduplicatedArray.length; i++) { nums[i] = deduplicatedArray[i]; } nums.length = deduplicatedArray.length; return nums.length; }; 중복제거한 array를 만들..

[leetcode, JS] 14. Longest Common Prefix

문제 문제 링크 : Longest Common Prefix 틀린 풀이 /** * @param {string[]} strs * @return {string} */ var longestCommonPrefix = function(strs) { let shortStr = strs[0] strs.forEach(el => { if(shortStr.length > el.length){ shortStr = el } }) let result = ""; shortStr = shortStr.split('') let chk = 0 shortStr.forEach(e => { strs.forEach(el => { if(el.includes(e)){ chk ++ } }) if(strs.length === chk) result +=..