-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path977.cpp
More file actions
31 lines (31 loc) · 664 Bytes
/
977.cpp
File metadata and controls
31 lines (31 loc) · 664 Bytes
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
class Solution {
public:
vector<int> sortedSquares(vector<int>& A) {
int n = A.size();
int j = 0;
while(j<n && A[j]<0)
j++;
int i = j-1;
vector<int> res(n);
int t = 0;
while(i>=0 && j<n){
if(A[i]*A[i] < A[j]*A[j]){
res[t++] = A[i]*A[i];
i--;
}
else{
res[t++] = A[j]*A[j];
j++;
}
}
while(i>=0){
res[t++] = A[i]*A[i];
i--;
}
while(j<n){
res[t++] = A[j]*A[j];
j++;
}
return res;
}
};