Created
December 3, 2020 21:27
-
-
Save kingsamadesu/65e53705dfb91eda92039b2d61a160bf to your computer and use it in GitHub Desktop.
977. Squares of a Sorted Array -with java- (leetcode.com)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Your runtime beats 100.00 % of java submissions. | |
Your memory usage beats 66.62 % of java submissions. | |
*/ | |
class Solution { | |
public int abs(int a){ | |
if(a >= 0){return a;} | |
return -a; | |
} | |
public int max(int a, int b){ | |
if(a>=b){ | |
return a; | |
} | |
return b; | |
} | |
public int[] sortedSquares(int[] A) { | |
int[] returnedArray = new int[A.length]; | |
int right = A.length-1; | |
int left = 0; | |
for(int i=0 ; i < A.length ; i++){ | |
if(abs(A[left])<abs(A[right])){ | |
returnedArray[A.length-i-1] = A[right]*A[right]; | |
right--; | |
}else{ | |
returnedArray[A.length-i-1] = A[left]*A[left]; | |
left++; | |
} | |
} | |
return returnedArray; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment