当前位置:网站首页>2022-7-6 Leetcode 977.有序数组的平方
2022-7-6 Leetcode 977.有序数组的平方
2022-07-07 11:36:00 【weixin_51187533】

为什么循环条件要设置 i <= j?
因为最后是 i 和 j 同时指向最小的数字。
class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
vector<int> result(nums.size(), 0);
int k = nums.size()-1;
int i = 0, j = nums.size()-1;
while (i <= j){
if (nums[i]*nums[i] < nums[j]*nums[j]){
result[k--] = nums[j]*nums[j];
j--;
}else {
result[k--] = nums[i]*nums[i];
i++;
}
}
return result;
}
};
边栏推荐
猜你喜欢
随机推荐
Mongodb replication (replica set) summary
Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
JS determines whether an object is empty
MongoDB的导入导出、备份恢复总结
【等保】云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
Thread pool reject policy best practices
DETR介绍
Getting started with cinnamon applet
Read PG in data warehouse in one article_ stat
MySQL error 28 and solution
Simple and easy-to-use code specification
Ogre introduction
JNA learning notes 1: Concepts
Ways to improve the performance of raspberry pie
cmake 学习使用笔记(一)
Custom thread pool rejection policy
Unity build error: the name "editorutility" does not exist in the current context
线程池拒绝策略最佳实践
MySQL error 28 and solution
Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules







![[learning notes] zkw segment tree](/img/18/21f455a06e8629243fc5cf4df0044c.png)

![SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1](/img/10/6de1ee8467b18ae03894a8d5ba95ff.png)