Skip to content

Commit f7dae3b

Browse files
committed
Create: 977-Squares-of-a-Sorted-Array.cpp
1 parent 9e67088 commit f7dae3b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Given an integer array nums sorted in non-decreasing order, return
3+
an array of the squares of each number sorted in non-decreasing order.
4+
5+
Ex.
6+
Input: nums = [-4,-1,0,3,10]
7+
Output: [0,1,9,16,100]
8+
9+
1.- Multiply each number of the nums by themselves.
10+
2.- Use the sort function to sort the vector.
11+
12+
Time: O(NlogN)
13+
Space: O(N)
14+
*/
15+
16+
class Solution {
17+
public:
18+
vector<int> sortedSquares(vector<int>& nums) {
19+
for (int& i : nums)
20+
i *= i;
21+
sort(nums.begin(), nums.end());
22+
return nums;
23+
}
24+
};

0 commit comments

Comments
 (0)