当前位置:网站首页>2022-7-6 Leetcode 977. Square of ordered array
2022-7-6 Leetcode 977. Square of ordered array
2022-07-07 13:39:00 【weixin_ fifty-one million one hundred and eighty-seven thousand】

Why should the cycle condition be set i <= j?
Because in the end i and j At the same time, point to the smallest number .
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;
}
};
边栏推荐
猜你喜欢

Distributed transaction solution

Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)

PAcP learning note 1: programming with pcap

干货|总结那些漏洞工具的联动使用

My "troublesome" subordinates after 00: not bad for money, against leaders, and resist overtime

Esp32 construction engineering add components

Realize the IP address home display function and number home query

.net core 关于redis的pipeline以及事务

How to make join run faster?

1、深拷贝 2、call apply bind 3、for of for in 区别
随机推荐
Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
分布式事务解决方案
Cmake learning and use notes (1)
LeetCode_ Binary search_ Medium_ 153. Find the minimum value in the rotation sort array
10 pictures open the door of CPU cache consistency
QQ medicine, Tencent ticket
室內ROS機器人導航調試記錄(膨脹半徑的選取經驗)
SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1
MongoDB 分片总结
xshell连接服务器把密钥登陆改为密码登陆
Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules
作战图鉴:12大场景详述容器安全建设要求
云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
Mongodb command summary
提升树莓派性能的方法
Clion mingw64 Chinese garbled code
Deep understanding of array related problems in C language
MySQL error 28 and solution
1. Deep copy 2. Call apply bind 3. For of in differences