본문 바로가기

Leetcode

(106)
[Leetcode] 189. Rotate Array - JS https://leetcode.com/problems/rotate-array/?envType=study-plan-v2&envId=top-interview-150  1. 문제 분석문제분석--------------------- 정수 배열 nums- k 단계만큼 오른쪽으로 회전시켜라.- 반환하지 말고 nums를 제자리에서 수정해라.제한사항--------------------- 1   2. 접근 방법 및 풀이✅ Key Pointk 스텝만큼 이동한다는 의미에서 유추할 수 있는 부분은 k 가 nums.length 보다 큰 경우는 nums.length로 나눈 나머지와 같다.k = k % nums.length 를 도출할 수 있다.즉, nums.length = 7일 때, k = 3,10,17,24.. 모두 동일하다고 볼..
[Leetcode] 169. Majority Element - JS https://leetcode.com/problems/majority-element/description/?envType=study-plan-v2&envId=top-interview-150  1. 문제 분석문제분석--------------------- 정수 배열 nums 속 다수인 요소를 반환하라.- 다수인 요소는 n / 2 이상 등장하는 요소이다.제한사항--------------------- n == nums.length- 1   2. 접근 방법해쉬 테이블 사용하여 갯수를 측정하고majority 변수에 해쉬 테이블에서 갯수를 비교하여 더 많은 요소를 할당한다.  풀이/** * @param {number[]} nums * @return {number} */var majorityElement = funct..
[Leetcode] 80. Remove Duplicates from Sorted Array II - JS https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/description/?envType=study-plan-v2&envId=top-interview-150  1. 문제 분석문제분석--------------------- 오름차순 정렬된 정수 배열 nums- 제자리에서 중복된 요소를 제거하라- 단, 최대 2번까진 중복이 가능하다. - 결과값으로 나온 nums 배열의 요소 갯수(k)를 반환하라.- 상대적인 순서는 유지한다.- nums 배열의 길이는 변하지 않는다.- 다른 배열에 추가 공간을 사용하지 말라. 공간 복잡도: O(1)제한사항--------------------- 1   2. 접근 방법해쉬 테이블 사용------------..
[Leetcode] 26. Remove Duplicates from Sorted Array - JS https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/?envType=study-plan-v2&envId=top-interview-150  1. 문제 분석문제분석--------------------- 오름차순 정렬된 정수 배열 nums이 주어지고 중복 요소를 제자리에서 제거하여 고유한 요소는 한번만 등장한다.- 상대적인 순서는 유지한다.- 고유한 요소 갯수(k) 반환하라.제한사항--------------------- 1   2. 접근 방법nums는 오름차순 정렬되어 있다고 하니중복 요소를 저장하는 변수 duplicated, 포인터 k를 가지고 순회하면서 - 요소가 다르면 duplicated를 수정 후 포인터에 dupli..
[Leetcode] 27. Remove Element - JS https://leetcode.com/problems/remove-element/description/?envType=study-plan-v2&envId=top-interview-150  1. 문제 분석문제분석----------------------------정수배열 nums와 정수 val이 주어진다.nums 배열 속 제자리에 있는 val을 모두 제거하라.nums 배열의 순서는 변경될 수 있다.nums 배열에서 val과 다른 요소의 갯수(k)를 반환하라.제한조건----------------------------- 0 - 예시로 결과값의 nums에 '_'를 추가했지만 어떤 값이 들어가도 상관 없다.  2. 접근 방법포인터를 활용1. 첫번째 순회 때, val과 같은 값은 '_'로 수정하면서 k 구함2. 두번..
[Leetcode] 88. Merge Sorted Array - JS https://leetcode.com/problems/merge-sorted-array/description/?envType=study-plan-v2&envId=top-interview-150 1. 문제 분석조건- nums1, nums2 정수 배열 주어진다. - 오름차순 정렬이다.- nums1과 nums2의 요소 갯수를 의미하는 m, n 정수가 주어진다.결과 -> nums1, nums2를 오름차순 정렬로 병합해라. 단, 새로운 배열을 반환하지 말고 nums1 배열을 수정하라.제한사항- nums1.length == m+n- nums2.length == n- 0   2.  접근 방법2-1. sort 정렬 방법non-decreasing order를 오름차순으로 생각하여 오름차순 정렬된 두 배열 있다고 ..
[Leetcode]_238. Product of Array Except Self 문제 https://leetcode.com/problems/product-of-array-except-self/description/?envType=study-plan-v2&envId=top-interview-150 LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 풀이 접근 방법은 다음과 같다. // 오답코드! def productExceptSelf(nums)..
[Leetcode]_380. Insert Delete GetRandom O(1) 문제 https://leetcode.com/problems/insert-delete-getrandom-o1/?envType=study-plan-v2&envId=top-interview-150 LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 풀이 기존 메서드를 활용하거나 for 문을 순회하면 쉽게 풀 수 있을 것 같은데 조건에서 모든 메서드는 O(1)의 시간 복..