Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int[] sortedSquares(int[] A) {
- int n = A.length;
- int[] squares = new int[n];
- int index = 0;
- // found first positive >= 0
- int pos = 0;
- while (pos < n && A[pos] < 0) {
- pos++;
- }
- int left = pos - 1;
- int right = pos;
- while (left >= 0 || right < n) {
- if (left < 0) {
- int absRight = Math.abs(A[right]);
- squares[index] = absRight * absRight;
- right++;
- index++;
- continue;
- }
- if (right >=n) {
- int absLeft = Math.abs(A[left]);
- squares[index] = absLeft * absLeft;
- left--;
- index++;
- }
- int absLeft = Math.abs(A[left]);
- int absRight = Math.abs(A[right]);
- if (absLeft < absRight) {
- squares[index] = absLeft * absLeft;
- left--;
- index++;
- } else {
- squares[index] = absRight * absRight;
- right++;
- index++;
- }
- }
- return squares;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement