当前位置:网站首页>力扣977. 有序数组的平方
力扣977. 有序数组的平方
2022-06-30 04:45:00 【智慧的人不要秃头】
给你一个按 非递减顺序 排序的整数数组 nums
,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。
方法:双指针,分别指向数组的首尾
class Solution {
public:
/*以下方法简单,但是不推荐,时间复杂度是 O(n + nlogn)*/
// vector<int> sortedSquares(vector<int>& nums) {
// for(int i = 0; i < nums.size(); ++i) {
// nums[i] *= nums[i];
// }
// sort(nums.begin(),nums.end()); 快速排序
// return nums;
// }
/*双指针法,由于数组是递增顺序排列的,平方后最大的数一定不是在最左边就是在最右边,不可能在中间
所以我们从两边进行平方比较*/
vector<int> sortedSquares(vector<int>& nums) {
vector<int> result(nums.size(), 0);
int k = nums.size() - 1;
//i指向数组的开始位置,j指向数组的终止位置
for(int i = 0, j = nums.size() - 1; i <= j;) { //i <= j,是为了考虑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;
}
//时间复杂度:O(n)
};
边栏推荐
- Redis实现短信登入功能(二)Redis实现登入功能
- Threadlocal
- Circle center technology, very anxious?
- Connect to the database and run node JS running database shows that the database is missing
- What is multimodal interaction?
- Qos(Quality of Service)
- Enter the date format string as the production date of the commodity, and enter the shelf life (days); Calculate the number of days until today before the expiration date of the product. 1. Change the
- Check London attractions suitable for parents and children in winter vacation
- One interview question and one joint index every day
- The most comprehensive summary notes of redis foundation + advanced project in history
猜你喜欢
This connection is not a private connection this website may be pretending to steal your personal or financial information
【Paper】2016_ A Learning-Based Fault Tolerant Tracking Control of an Unmanned Quadrotor Helicopter
How to apply for SSL certificate from the manufacturer
Break through the existing customer group marketing, and try customer grouping management (including clustering model and other practical effect evaluation)
MySQL查询小工具(一)json格式的字符串字段中,替换json数组中对象的某个属性值
Transport layer protocol tcp/udp
Beanfactory creation process
Unreal 4 learning notes - data storage using blueprints
Photon pun refresh hall room list
Unity automatic pathfinding
随机推荐
2021-03-16
Recommended cultural landmarks of these tourist attractions in Bangkok
Unity automatic pathfinding
深度学习------不同方法实现Inception-10
Redis实现短信登入功能(一)传统的Session登入
【Paper】2013_ An efficient model predictive control scheme for an unmanned quadrotor helicopter
【Paper】2021_ Observer-Based Controllers for Incrementally Quadratic Nonlinear Systems With Disturbanc
Interprocess communication
The role of break
EasyRecovery数据恢复软件 恢复了我两年前的照片视频数据
Unity enables simple music visualization
Array of small C
brew安装nvm报nvm command not found解决方案
Redis implements SMS login function (II) redis implements login function
Thread safety and processing caused by multithreading
[control] multi agent system summary. 1. system model. 2. control objectives. 3. model transformation.
The difference between get and post requests
圆心科技,很焦虑?
【Paper】2020_ Research on defense and evaluation strategy of heterogeneous UAV formation_ Zuojiankai
Network high concurrency