문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음
오름차순으로 정렬되어 있는 정수 배열 nums가 주어졌을 때, 각 숫자의 제곱값을 오름차순으로 정렬한 배열을 반환해라.
#1
Input: nums = [-4, -1, 0, 3, 10]
Output: [0, 1, 9, 16, 100]
Explanation: 제곱 후, 배열은 [16, 1, 0, 9, 100]이다.
정렬 후, [0, 1, 9, 16, 100]이다.
#2
Input: nums = [-7, -3, 2, 3, 11]
Output: [4, 9, 9, 49, 121]
class Solution {
public int[] sortedSquares(int[] nums) {
int[] result = new int[nums.length];
int left = 0;
int right = nums.length - 1;
for(int i = nums.length - 1; i >= 0; i--){
if(Math.abs(nums[left]) > Math.abs(nums[right])){
result[i] = nums[left] * nums[left];
left++;
}else{
result[i] = nums[right] * nums[right];
right--;
}
}
return result;
}
}