본문 바로가기

Algorithm

(38)
[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 !..
[LeetCode] First Bad Version - 이진탐색 (binary search) 문제 You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad. Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to..
[LeetCode] Merge Sorted Array 문제 You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. 주어진 배열 nums1과 nums2는 순서대로 정렬되어있으며, Integer m과 n은 각각 nums1과 nums2가 가진 element의 수를 의미한다. Merge nums1 and nums2 into a single array sorted in non-decreasing order. nums1과 nums2를 합치고 오름차순으로 정렬하라. The final sorted array sho..