【LeetCode每日一题】[简单]977. 有序数组的平方

【LeetCode每日一题】[简单]977. 有序数组的平方

977. 有序数组的平方

题目来源
算法思想:数组

题目:
在这里插入图片描述
思路:将数值平方后记录在新数组中;将数组进行排序

java代码

class Solution {
    
    
    public int[] sortedSquares(int[] A) {
    
    
		int[] B = new int[A.length];//新建一个数组
		for (int i = 0; i < B.length; i++) {
    
    
			B[i] = A[i] * A[i];//存储A数组中数平方后的结果
		}
		Arrays.sort(B);//对数组进行排序
		return B;//返回
    }
}

猜你喜欢

转载自blog.csdn.net/qq_39457586/article/details/109114358