본문 바로가기

Web development131

[LeetCode] Reverse Integer leetcode.com/explore/interview/card/top-interview-questions-easy/127/strings/880/ 포인트는 x를 "뒤집은 후"의 값이 -2^31 ~ 2^31 - 1 을 벗어나는 경우 0을 리턴하는 것이다. 실제로 javascript에서 핸들링 가능한 수는 -2^53 ~ 2^53 -1 이라고 한다. (tip. 외울 필요 없이, Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER로 접근 가능하다.) 그러므로 범위 내라면 integer로 계산 가능하기 때문에, Plus One 문제처럼 항상 string으로 다룰 필요는 없다. 범위값은 Math.pow함수를 사용해서 구하고, -를 제거하기 위해 Math.abs를 사용하면 간단하다.. 2021. 1. 20.
[LeetCode] Rotate Image, Reverse String Rotate Image leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/770/ 굉장히 힘든 문제였다. 하나하나 자리 바꾸는 방식부터 살펴보자면 3중 for문을 사용하고 있는데, for문들은 사실 반복 횟수를 제한해주는 역할이라 중첩을 줄인다고 해서 효율이 올라가진 않기 때문에 효율 측면에서 그렇게 나쁘지는 않을것 같기는 하다. 다만 구현이 너무 헷갈리고 힘들었기 때문에 실전에서는 이렇게 할 수 있을까 하는 의문이 든다. 사실 문제에 제시된 이미지처럼 실제로 하나씩 옮겨야한다는 생각에 갇혀서 어렵게 풀었는데, Input Output을 자세히 들여다보니 좀 더 쉬운 방법이 보였다. [[1,2,3],[4,5,6],[7,8,9.. 2021. 1. 20.
[LeetCode] Valid Sudoku 주어진 2차원 배열 형태의 스도쿠 문제가 유효한지 리턴하는 문제다. leetcode.com/explore/featured/card/top-interview-questions-easy/92/array/769/ Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 풀이 /** * @param {character[][]} board * @return {boolea.. 2021. 1. 15.
[LeetCode] Move Zeros, Two Sum Move Zeros leetcode.com/explore/featured/card/top-interview-questions-easy/92/array/567/ 배열에서 0을 모두 찾아 맨 뒤로 옮기면 된다. // https://leetcode.com/explore/featured/card/top-interview-questions-easy/92/array/567/ /** * @param {number[]} nums * @return {void} Do not return anything, modify nums in-place instead. */ var moveZeroes = function (nums) { let count = 0; let len = nums.length; for (let i = 0; i.. 2021. 1. 15.
[LeetCode] Plus One 숫자에 1을 더해서 리턴하면 되는 간단한 문제이지만, 배열로 쪼개져 있으며 숫자의 길이가 최대 100이므로 int로 변환해 1을 더하는 방식은 사용할 수 없다. leetcode.com/explore/featured/card/top-interview-questions-easy/92/array/559/ /** * @param {number[]} digits * @return {number[]} */ var plusOne = function(digits) { // 1899 -> 뒤에서부터 반복을 돌며 9면 0, 아니면 +1한다. => 1900 // 99 -> 뒤에서부터 반복을 돌며 9면 0으로 바꾸고, 마지막에도 9라면 1을 처음에 추가한다. => 100 const last = digits.length - 1.. 2021. 1. 12.
[LeetCode] Single Number, Intersection of Two Arrays II Single Number 배열 요소에 짝 없이 혼자 있는 요소를 찾는 문제. 하나씩 뽑아서 짝을 찾아 제거하는 방식으로 풀었는데, 좀 더 좋은 방법이 없을까... leetcode.com/explore/featured/card/top-interview-questions-easy/92/array/549/ /** * @param {number[]} nums * @return {number} */ var singleNumber = function(nums) { // 반복문은 최대로 돌아도 주어진 배열 길이의 반만큼만 돌면 된다. const count = Math.floor(nums.length/2); for(let i = 0; i < count; i++){ const item = nums.shift(); con.. 2021. 1. 12.
[LeetCode] Rotate Array, Contains Duplicate Rotate Array leetcode.com/explore/featured/card/top-interview-questions-easy/92/array/646/ /** * @param {number[]} nums * @param {number} k * @return {void} Do not return anything, modify nums in-place instead. */ var rotate = function (nums, k) { // for(let i = 0; i < k; i++) { // const pop = nums.pop() // nums.unshift(pop) // } // return nums; const spliced = nums.splice(nums.length - k, nums... 2021. 1. 8.
[LeetCode] Remove Duplicates From Sorted Array https://leetcode.com/problems/remove-duplicates-from-sorted-array/ /** * @param {number[]} nums * @return {number} */ var removeDuplicates = function (nums) { const arr = Array.from(new Set(nums)); nums.splice(0, arr.length, ...arr); return arr.length; }; 처음엔 return [...(new Set(nums))].length 라고 했다가 틀려서 다시 읽어보니 단순히 중복을 제거하고 반환하는 것이 아니라 "Note that the input array is passed in by reference, which.. 2021. 1. 6.
[LeetCode] Best Time to Buy and Sell Stock II leetcode.com/explore/featured/card/top-interview-questions-easy/92/array/564/ /** * @param {number[]} prices * @return {number} */ var maxProfit = function(prices) { let profit = 0; for(const i in prices) { console.log('오늘의 가격', prices[i]); if (i !== 0 && prices[i-1] < prices[i]) { profit += prices[i] - prices[i-1]; console.log('판매함', prices[i]); console.log('이윤', prices[i] - prices[i-1]) } } co.. 2021. 1. 6.