当前位置:网站首页>【力扣】977. 有序数组的平方
【力扣】977. 有序数组的平方
2022-06-28 15:59:00 【e1373773】
这道题大致有两种方法,一种是先循环再排序,一种是双指针比较后排序。
大家平时工作中会用哪种方法,请评论区告诉up,谢谢。
方法一: 普通先循环再排序(PHP)
class Solution {
/**
* @param Integer[] $nums
* @return Integer[]
*/
function sortedSquares($nums) {
foreach($nums as &$value) {
$value = $value * $value;
}
unset($value);
asort($nums);
return $nums;
}
}解释: 利用PHP里的数组引用赋值 "&" 而非拷贝赋值方法,减少空间复杂度。
测试结果:
执行用时:72 ms, 在所有 PHP 提交中击败了59.44%的用户
内存消耗:18.3 MB, 在所有 PHP 提交中击败了6.29%的用户
通过测试用例:137 / 137
方法二: 利用双指针(PHP)
class Solution {
/**
* @param Integer[] $nums
* @return Integer[]
*/
function sortedSquares($nums) {
$length = count($nums) - 1;
$res = [];
$res = array_pad($res, $length, 1);
for($i = 0, $j = $length, $pos = $length; $i <= $j; ) {
if ($nums[$i] * $nums[$i] > $nums[$j] * $nums[$j]) {
$res[$pos] = $nums[$i] * $nums[$i];
++$i;
} else {
$res[$pos] = $nums[$j] * $nums[$j];
--$j;
}
--$pos;
}
return $res;
}
}解释: PHP在创建数组是不指定数组长度,是不是不能通过下标来插入元素。。。这里使用了 array_pad 来指定数组长度并且填充了占位元素。
测试结果:
执行用时:64 ms, 在所有 PHP 提交中击败了86.01%的用户
内存消耗:17.8 MB, 在所有 PHP 提交中击败了69.23%的用户
通过测试用例:137 / 137
边栏推荐
- wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe-Module
- RedmiBook Pro 14增强版 打不开台达软件DRAStudio_v1.00.07.52
- Etcd visualization tool: an introduction to kstone (I)
- LDD 知识整理
- 【Hot100】4. 寻找两个正序数组的中位数
- What is the maximum number of concurrent TCP connections for a server? 65535?
- 麻烦问一下,我数据库中写入占99%查询很少,用按量付费模式还是预留模式比较好?
- Big God explains open source buff gain strategy live lecture
- 关注35岁的坎:畏惧是因为你没有匹配这个年纪该有的能力
- Tiktok actual battle ~ list of bloggers I follow, follow and check
猜你喜欢

Visual Studio 2019软件安装包和安装教程

10:00面试,10:02就出来了 ,问的实在是太...

Knowing these commands allows you to master shell's own tools

知道这几个命令让你掌握Shell自带工具

Big God explains open source buff gain strategy live lecture

超自动化与网络安全的未来

全球陆续拥抱Web3.0,多国已明确开始抢占先机

Openharmony - detailed source code of Kernel Object Events

Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse

【Hot100】1. 两数之和
随机推荐
【推荐系统】多任务学习之ESMM模型(更新ing)
部门新来了个字节25K出来的,让我见识到了什么是天花板
MySQL self connection query "suggestions collection"
面试官: 线程池是如何做到线程复用的?有了解过吗,说说看
Change exchange (dynamic planning)
Soliciting articles and contributions - building a blog environment with a lightweight application server
Navicat 15 for MySQL
IPDK — Overview
【Hot100】3. 无重复字符的最长子串
NAACL 2022 | 机器翻译SOTA模型的蒸馏
[MySQL] official website document learning query statement SQL precautions
among us私服搭建
CODING DevOps 助力中化信息打造新一代研效平台,驱动“线上中化”新未来
Open source technology exchange - Introduction to Chengying, a one-stop fully automated operation and maintenance manager
Slim gain (sgain) introduction and code implementation -- missing data filling based on generated countermeasure network
MySQL auto - Connect Query recommended favorites
Cross cluster deployment of helm applications using karmada
QT create 5.0.3 configuring qt4.8.7
Lenet5 training model of convolutional neural network for machine learning
Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills