Skip to content

Commit d1819dc

Browse files
authored
Update squares-of-a-sorted-array.cpp
1 parent f1696f1 commit d1819dc

File tree

1 file changed

+3
-12
lines changed

1 file changed

+3
-12
lines changed

C++/squares-of-a-sorted-array.cpp

+3-12
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,16 @@ class Solution {
1111
int left = right - 1;
1212

1313
vector<int> result;
14-
while (0 <= left && right < A.size()) {
15-
if (A[left] * A[left] < A[right] * A[right]) {
14+
while (0 <= left || right < A.size()) {
15+
if (right == A.size() ||
16+
(0 <= left && A[left] * A[left] < A[right] * A[right])) {
1617
result.emplace_back(A[left] * A[left]);
1718
--left;
1819
} else {
1920
result.emplace_back(A[right] * A[right]);
2021
++right;
2122
}
2223
}
23-
24-
while (left >= 0) {
25-
result.emplace_back(A[left] * A[left]);
26-
--left;
27-
}
28-
while (right < A.size()) {
29-
result.emplace_back(A[right] * A[right]);
30-
++right;
31-
}
32-
3324
return result;
3425
}
3526
};

0 commit comments

Comments
 (0)