当前位置:网站首页>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;
}
};
边栏推荐
- RecyclerView的数据刷新
- 分布式事务解决方案
- DrawerLayout禁止侧滑显示
- Talk about pseudo sharing
- Mongodb meets spark (for integration)
- Write it down once Net a new energy system thread surge analysis
- 最佳实践 | 用腾讯云AI意愿核身为电话合规保驾护航
- Custom thread pool rejection policy
- Toraw and markraw
- Cloud detection 2020: self attention generation countermeasure network for cloud detection in high-resolution remote sensing images
猜你喜欢
Getting started with MySQL
Redis只能做缓存?太out了!
MongoDB内部的存储原理
C语言数组相关问题深度理解
Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
Fast development board pinctrl and GPIO subsystem experiment for itop-imx6ull - modify the device tree file
Ways to improve the performance of raspberry pie
QQ的药,腾讯的票
Scrapy教程经典实战【新概念英语】
【黑马早报】华为辟谣“军师”陈春花;恒驰5预售价17.9万元;周杰伦新专辑MV 3小时播放量破亿;法华寺回应万元月薪招人...
随机推荐
Getting started with MySQL
提升树莓派性能的方法
Clion mingw64 Chinese garbled code
Fast development board pinctrl and GPIO subsystem experiment for itop-imx6ull - modify the device tree file
Write it down once Net a new energy system thread surge analysis
我那“不好惹”的00后下属:不差钱,怼领导,抵制加班
Cloud detection 2020: self attention generation countermeasure network for cloud detection in high-resolution remote sensing images
Esp32 series column
QQ medicine, Tencent ticket
Milkdown 控件图标
Error lnk2019: unresolved external symbol
PAcP learning note 1: programming with pcap
10 张图打开 CPU 缓存一致性的大门
User management summary of mongodb
LED light of single chip microcomputer learning notes
[learning notes] segment tree selection
记一次 .NET 某新能源系统 线程疯涨 分析
[dark horse morning post] Huawei refutes rumors about "military master" Chen Chunhua; Hengchi 5 has a pre-sale price of 179000 yuan; Jay Chou's new album MV has played more than 100 million in 3 hours
Distributed transaction solution
Data refresh of recyclerview