Leetcode PHP题解--D34 977. Squares of a Sorted Array

977. Squares of a Sorted Array

题目连接

977. Squares of a Sorted Arrayphp

题目分析

本题比较简单。对给定数组的每个数字的平方。并对结果进行排序。数组

思路

遍历每个元素,相乘自身。塞入新数组。函数

再用sort函数排序。.net

最终代码

<?php
class Solution {
    function sortedSquares($A) {
        $z = [];
        foreach($A as $b){
            $z[] = $b*$b;
        }
        sort($z);
        return $z;
    }
}

若以为本文章对你有用,欢迎用爱发电资助。code

相关文章
相关标签/搜索