Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 |
30 | 31 |
Tags
- 코딩테스트
- IT기초
- leetcode
- 회사폐업
- 청년내일채움공제
- 자료구조
- 알고리즘
- 정보처리기사개정
- 동적계획법
- array
- 전화영어
- 실업인정인터넷신청
- 프로그래머스
- 캡쳐링
- 생애첫계약
- 정보
- C++
- 부분합알고리즘
- 실업급여
- 후니의쉽게쓴시스코네트워킹
- 후니의쉽게쓴시스코라우팅
- 자취준비
- 막대기자르기
- 사회초년생
- 튜터링
- 모여봐요동물의숲
- 취업사실신고
- 순열
- 네트워크
- HeadFirstDesignPatterns
Archives
- Today
- Total
따봉도치야 고마워
[LeetCode] Arrays 101: 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]
Output: [0,1,9,16,100]
Explanation: After squaring, the array becomes [16,1,0,9,100].
After sorting, it becomes [0,1,9,16,100].
Example 2:
Input: nums = [-7,-3,2,3,11]
Output: [4,9,9,49,121]
Constraints:
- 1 <= nums.length <= 104
- -104 <= nums[i] <= 104
- nums is sorted in non-decreasing order.
Follow up: Squaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach?
Code
var sortedSquares = function(nums) {
return nums.map((n) => n * n).sort((a, b) => a - b);
};
'프로그래밍 > 알고리즘' 카테고리의 다른 글
[LeetCode] Arrays 101: Merge Sorted Array (0) | 2022.04.07 |
---|---|
[LeetCode] Arrays 101: Duplicate Zeros (0) | 2022.04.07 |
[LeetCode] Arrays 101: Find Numbers with Even Number of Digits (0) | 2022.04.05 |
[LeetCode] Arrays 101: Max Consecutive Ones (0) | 2022.04.05 |
[프로그래머스] 모의고사 (0) | 2021.10.17 |
Comments