Squares of a Sorted Array有序数组的平方
Description
Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.
public class Solution {
/**
* @param A: The array A.
* @return: The array of the squares.
*/
public int[] SquareArray(int[] A) {
// write your code here
if(A== null || A.length == 0){
return null ;
}
int[] result = new int[A.length] ;
for(int i = 0 ; i < A.length ; i++ ){
result[i] = A[i] * A[i] ;
}
Arrays.sort(result) ;
return result ;
}
}
该篇博客介绍了一个Java方法,用于将非递减有序数组的元素平方后,保持平方后的结果依然有序。方法首先计算每个元素的平方,然后使用`Arrays.sort()`进行排序,最终返回平方后的有序数组。
245

被折叠的 条评论
为什么被折叠?



