当前位置:网站首页>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;
}
};
边栏推荐
- 【面试高频题】难度 2.5/5,简单结合 DFS 的 Trie 模板级运用题
- [learning notes] agc010
- MongoDB复制(副本集)总结
- Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules
- 室內ROS機器人導航調試記錄(膨脹半徑的選取經驗)
- C语言数组相关问题深度理解
- Milkdown 控件图标
- RealBasicVSR测试图片、视频
- 【黑马早报】华为辟谣“军师”陈春花;恒驰5预售价17.9万元;周杰伦新专辑MV 3小时播放量破亿;法华寺回应万元月薪招人...
- User management summary of mongodb
猜你喜欢
Storage principle inside mongodb
Indoor ROS robot navigation commissioning record (experience in selecting expansion radius)
Esp32 construction engineering add components
【黑马早报】华为辟谣“军师”陈春花;恒驰5预售价17.9万元;周杰伦新专辑MV 3小时播放量破亿;法华寺回应万元月薪招人...
Error lnk2019: unresolved external symbol
Talk about pseudo sharing
室内ROS机器人导航调试记录(膨胀半径的选取经验)
记一次 .NET 某新能源系统 线程疯涨 分析
如何让join跑得更快?
10 张图打开 CPU 缓存一致性的大门
随机推荐
How to make join run faster?
Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
C语言数组相关问题深度理解
[Presto profile series] timeline use
Why can basic data types call methods in JS
分屏bug 小记
线程池拒绝策略最佳实践
JS function returns multiple values
聊聊伪共享
OSI seven layer model
toRaw和markRaw
MongoDB 遇见 spark(进行整合)
Vscode编辑器ESP32头文件波浪线不跳转彻底解决
存储过程的介绍与基本使用
LED light of single chip microcomputer learning notes
Getting started with cinnamon applet
Redis只能做缓存?太out了!
ROS机器人更换新雷达需要重新配置哪些参数
648. 单词替换 : 字典树的经典运用
Use of polarscatter function in MATLAB