class Solution {
public int[] sortedSquares(int[] nums) {
//双指针
int right = nums.length-1;
int left = 0;
int[] ans = new int[nums.length];
for(int i = nums.length-1; i>=0; i--){
if(Math.pow(nums[right],2) >= Math.pow(nums[left],2)){
System.out.print("1");
ans[i] = (int)Math.pow(nums[right], 2);
right --;
}else{
ans[i] = (int)Math.pow(nums[left], 2);
left++;
}
}
return ans;
}
}
977. Squares of a Sorted Array
最新推荐文章于 2024-08-12 19:18:27 发布