当前位置:网站首页>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;
}
};
边栏推荐
- RealBasicVSR测试图片、视频
- MongoDB命令汇总
- LeetCode_二分搜索_中等_153.寻找旋转排序数组中的最小值
- Cinnamon taskbar speed
- Drawerlayout suppress sideslip display
- Storage principle inside mongodb
- My "troublesome" subordinates after 00: not bad for money, against leaders, and resist overtime
- move base参数解析及经验总结
- High end for 8 years, how is Yadi now?
- Use of polarscatter function in MATLAB
猜你喜欢

LIS 最长上升子序列问题(动态规划、贪心+二分)

存储过程的介绍与基本使用

QQ的药,腾讯的票

10 pictures open the door of CPU cache consistency

About how appium closes apps (resolved)

1. Deep copy 2. Call apply bind 3. For of in differences

1、深拷贝 2、call apply bind 3、for of for in 区别

单片机原理期末复习笔记

Digital IC Design SPI

Final review notes of single chip microcomputer principle
随机推荐
靠卖概念上市,认养一头牛能走多远?
JS function returns multiple values
信号强度(RSSI)知识整理
LED light of single chip microcomputer learning notes
Vscade editor esp32 header file wavy line does not jump completely solved
Mongodb slice summary
如何让join跑得更快?
Deep understanding of array related problems in C language
记一次 .NET 某新能源系统 线程疯涨 分析
学习突围2 - 关于高效学习的方法
Distributed transaction solution
Clion mingw64 Chinese garbled code
error LNK2019: 无法解析的外部符号
MongoDB 遇见 spark(进行整合)
自定义线程池拒绝策略
Centso7 OpenSSL error Verify return code: 20 (unable to get local issuer certificate)
Cinnamon taskbar speed
Indoor ROS robot navigation commissioning record (experience in selecting expansion radius)
Detr introduction
Talk about pseudo sharing