美文网首页Leetcode题解-PHP版
Leetcode PHP题解--D34 977. Squares

Leetcode PHP题解--D34 977. Squares

作者: skys215 | 来源:发表于2019-04-17 04:20 被阅读0次

977. Squares of a Sorted Array

题目链接

977. Squares of a Sorted Array

题目分析

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

思路

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

再用sort函数排序。

最终代码

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

若觉得本文章对你有用,欢迎用爱发电资助。

相关文章

网友评论

    本文标题:Leetcode PHP题解--D34 977. Squares

    本文链接:https://www.haomeiwen.com/subject/sriwwqtx.html