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
- 생애첫계약
- 프로그래머스
- C++
- 후니의쉽게쓴시스코네트워킹
- 정보
- 자료구조
- 사회초년생
- HeadFirstDesignPatterns
- 튜터링
- leetcode
- 취업사실신고
- 순열
- 실업인정인터넷신청
- 회사폐업
- 부분합알고리즘
- 모여봐요동물의숲
- 캡쳐링
- 알고리즘
- 막대기자르기
- 동적계획법
- 코딩테스트
- 후니의쉽게쓴시스코라우팅
- IT기초
- 자취준비
- 전화영어
- 청년내일채움공제
- array
- 실업급여
- 네트워크
- 정보처리기사개정
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