当前位置:网站首页>Force buckle 977 Square of ordered array
Force buckle 977 Square of ordered array
2022-06-30 04:56:00 【A wise man should not be bald】
Here's a button Non decreasing order Sorted array of integers nums, return The square of each number A new array of , According to the requirements Non decreasing order Sort .

Method : Double pointer , Point to the beginning and end of the array respectively
class Solution {
public:
/* The following method is simple , But not recommended , The time complexity is 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()); Quick sort
// return nums;
// }
/* Double finger needling , Because the array is arranged in ascending order , The largest number after square must be either on the left or on the right , It can't be in the middle
So let's compare the squares from both sides */
vector<int> sortedSquares(vector<int>& nums) {
vector<int> result(nums.size(), 0);
int k = nums.size() - 1;
//i Point to the beginning of the array ,j Point to the end of the array
for(int i = 0, j = nums.size() - 1; i <= j;) { //i <= j, To consider i and j There is an element in the middle
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;
}
// Time complexity :O(n)
};边栏推荐
- Meet in Bangkok for a romantic trip on Valentine's Day
- Unity3d lookat parameter description
- Implementation of one interview question one distributed lock every day
- Unity3d realizes Google Digital Earth
- Unity script life cycle and execution sequence
- 為什麼win10開熱點後電腦沒有網絡?
- Unity lens making
- Issue SSL certificate with IP address
- Passing values between classes using delegates and events
- Winter vacation parent-child tour, these new york attractions are not only fun but also knowledge
猜你喜欢

The most comprehensive summary notes of redis foundation + advanced project in history

harbor api 2.0查询

Meet in Bangkok for a romantic trip on Valentine's Day

Unity realizes rotation and Revolution

Yolov5 torch installation

UE4 method of embedding web pages

On mask culling of unity

A virtual reality secret room escape adventure, let you see Technology Singapore

Oculus quest2 development: (I) basic environment construction and guide package

【Paper】2016_ A Learning-Based Fault Tolerant Tracking Control of an Unmanned Quadrotor Helicopter
随机推荐
The most comprehensive summary notes of redis foundation + advanced project in history
Detailed explanation of the process of "flyingbird" small game (camera adjustment and following part)
Meet in Bangkok for a romantic trip on Valentine's Day
Yolov5 torch installation
【Paper】2017_ Research on coordinated control method of underwater vehicle formation marine survey
This connection is not a private connection this website may be pretending to steal your personal or financial information
EasyRecovery数据恢复软件 恢复了我两年前的照片视频数据
Preorder traversal of Li Kou 589:n fork tree
Modbus protocol register
Webots notes day 2
力扣2049:统计最高分的节点数目
Unit screenshot saved on the phone
How does unity use mapbox to implement real maps in games?
Unity realizes rotation and Revolution
Foreign SSL certificate
Ripple effect of mouse click (unity & shader)
Passing values between classes using delegates and events
【Paper】2021_ Uniformity of heterogeneous hybrid multi-level intelligent systems using UGV and UAV
Unity3d realizes Google Digital Earth
【Paper】2021_ Observer-Based Controllers for Incrementally Quadratic Nonlinear Systems With Disturbanc