728x90
1. 문제(원본)
Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.
Example 1:
Input: [-4,-1,0,3,10] Output: [0,1,9,16,100]
Example 2:
Input: [-7,-3,2,3,11] Output: [4,9,9,49,121]
Note:
- 1 <= A.length <= 10000
- -10000 <= A[i] <= 10000
- A is sorted in non-decreasing order.
2. 문제
각 배열 인덱스의 제곱의 수를 정렬
3. 나의 답
class Solution {
func sortedSquares(_ A: [Int]) -> [Int] {
return A.map { $0 * $0 }.sorted { $0 < $1 }
}
}
4. 다른 유저의 답
#1
class Solution {
func sortedSquares(_ A: [Int]) -> [Int] {
return A.map { $0 * $0 }.sorted()
}
}
5. 마무리
sorted()는 자연스럽게 내림차순이라는 것을 배우게 되었다.
이번 문제는 배열의 함수를 적절히 활용하면 간단하게 풀 수 있는 문제였다.
728x90
'알고리즘 > 해결' 카테고리의 다른 글
LeetCode1295. Find Numbers with Even Number of Digits (0) | 2020.02.02 |
---|---|
LeetCode1313. Decompress Run-Length Encoded List (0) | 2020.02.02 |
LeetCode709. To Lower Case (0) | 2020.01.26 |
LeetCode 905. Sort Array By Parity (0) | 2020.01.19 |
LeetCode 1207. Unique Number of Occurrences (0) | 2020.01.19 |