일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- 기초
- leetcode977
- youtube iframe
- nvmrc
- JS
- react-native
- 커스텀알림
- 자바스크립트
- 타입스크립트
- 프로토타입
- react-firebaseui
- mac
- yarn-berry
- Spread
- Next.js
- Rest
- 커스텀알락
- css
- iP
- 구조분해할당
- 파이어베이스로그인
- React
- 다리놓기
- 백준
- 리액트
- 파이썬
- nvm
- Python
- leetcode189
- firebaseui
- Today
- Total
JadeCode
[Leetcode] Algorithm Day2 Two Pointers 본문
977. Squares of a Sorted Array
정렬하기. 하지만 sort내장함수 쓰지 않고 시간복잡도 O(n)으로 만들기
1. 일단 각 숫자들을 문제에 나와있는대로 제곱을 한다.
2. 선택 정렬 알고리즘 사용한다.
* 선택정렬 알고리즘
배열을 돌면서 최소값을 찾아간다. 현재 index와 최소값을 가진 index를 swap한다.
/**
* @param {number[]} nums
* @return {number[]}
*/
var sortedSquares = function (nums) {
nums = nums.map((el) => Math.abs(el) * Math.abs(el));
const swap = (a, b, nums) => {
[nums[a], nums[b]] = [nums[b], nums[a]];
return a, b;
};
for (let i = 0; i < nums.length; i++) {
min_index = i;
for (let j = i + 1; j < nums.length; j++) {
if (nums[min_index] > nums[j]) {
min_index = j;
}
}
swap(i, min_index, nums);
}
return nums;
};
https://leetcode.com/problems/squares-of-a-sorted-array/?envType=study-plan&id=algorithm-i
Squares of a Sorted Array - LeetCode
Can you solve this real interview question? Squares of a Sorted Array - Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order. Example 1: Input: nums = [-4,-1,0,3,10] Out
leetcode.com
189. Rotate Array
k번만큼 로테이트 시켜 배열을 만든다.
일단 k가 nums의 길이보다 크면 나머지값으로 구한다.
뒤에서부터 k만큼 자른 후 앞에 shift한다.
/**
* @param {number[]} nums
* @param {number} k
* @return {void} Do not return anything, modify nums in-place instead.
*/
var rotate = function (nums, k) {
k = k > nums.length ? k % nums.length : k;
const splice = nums.splice(nums.length - k, nums.length);
nums.unshift(...splice);
return nums;
};
https://leetcode.com/problems/rotate-array/?envType=study-plan&id=algorithm-i
Rotate Array - LeetCode
Can you solve this real interview question? Rotate Array - Given an integer array nums, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 step
leetcode.com