분류 전체보기 (62) 썸네일형 리스트형 [LeetCode] Two Sum 문제 Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. 풀이 /** * @param {number[]} nums * @param {number} target * @return {number[]} */ var twoSum = function(nums, target) { if (nu.. [LeetCode] Longest Common Prefix 문제 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 "". 배열로 주어진 문자열들의 가장 긴 prefix를 찾는 function을 작성하라. 공통 prefix가 없는 경우 빈 문자열 ""를 반환하여라 풀이 문자열 배열 중 첫번째 단어가 포함하는 알파벳을 기준으로 prefix를 찾는 방법으로 접근해보았다. const firstWord = Array.from(strs[0]); let prefix = ''; 주어진 배열의 첫 단어를 딴 변수 firstWord와 prefix가 확정된 알파벳을 저장할 변수 pre.. [LeetCode] Find the Index of the First Occurrence in a String 문제 Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if haystack is not part of haystack. needle과 haystack이라는 주어진 두 string값으로 needle이 haystack에 처음 등작하는 index를 반환하여라. needle이 haystack에 없는 경우 -1을 반환한다. 풀이 1. search() 활용 /** * @param {string} haystack * @param {string} needle * @return {number} */ var strStr = function(haystack, needle) { l.. [LeetCode] Valid Palindrome 문제 A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true if it is a palindrome, or false otherwise. 회문(回文)은 모든 대문자를 소문자로 변환하고 영숫자가 아닌 문자를 모두 제거한 후 앞뒤가 같은 경우 구문을 의미한다.주어진 문자열 s가 회문이면 true를 반환하고.. [LeetCode]First Unique Character in a String 문제 Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1. 주어진 문자열 s에서 반복되지 않는 첫번째 문자의 index를 반환하여라. 존재하지 않는다면, -1을 반환하라. 풀이 /** * @param {string} s * @return {number} */ var firstUniqChar = function(s) { const array = Array.from(s); const countMap = new Map(); for (let i = 0; i < array.length; i++) { countMap.set(array[i],{cnt : coun.. [LeetCode] Reverse Integer 문제 Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. Assume the environment does not allow you to store 64-bit integers (signed or unsigned). 풀이 음수인 경우 '-' 처리하는게 귀찮을 것 같아서 abs에 param으로 받은 x 의 절대값을 할당하고 시작했다. x의 절대값인 abs를 배열로 만들어 reverse()로 순서를 뒤집어 준 다음. 앞서 체크한 음수 여부에 따라.. [LeetCode] Move Zeroes 문제 Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in-place without making a copy of the array. 주어진 숫자 배열에서 0을 찾아 모두 지우고 배열 맨 마지막으로 이동시켜라. 배열의 복사본을 만들지 말고 주어진 배열 nums 안에서 해결할 것. Follow up: Could you minimize the total number of operations done? 풀이 /** * @param {number[]} nums * @return {void} Do .. [LeetCode] Count Primes 문제 Given an integer n, return the number of prime numbers that are strictly less than n. 주어진 숫자 n을 가지고 n보다 작은 숫자들 중 소수를 찾아 리턴하시오 풀이 에라토스테네스의 체 를 활용해 문제를 풀어야겠다고 생각하여. n보다 작은 숫자로 배열을 만들고, 2,3,5,7의 배수가 아닌 수를 순차적으로 filter해주는 방법을 생각하여 문제를 풀었다. /** * @param {number} n * @return {number} */ var countPrimes = function(n) { // prime numbers : 2, 3, 5, 7 if (n i + 1); array = array.filter((el) => el % 2 !.. 이전 1 2 3 4 5 ··· 8 다음