当前位置:网站首页>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)
};边栏推荐
- 力扣349. 两个数组的交集
- redis集群概念
- Oculus quest2 development: (I) basic environment construction and guide package
- svg和canvas的区别
- Force buckle 59 Spiral matrix II
- A collection of errors encountered in machine learning with unity
- [UAV] kinematic analysis from single propeller to four rotor UAV
- Easyrecovery data recovery software recovers my photo and video data two years ago
- 為什麼win10開熱點後電腦沒有網絡?
- Implementation of one interview question one distributed lock every day
猜你喜欢

Singapore must visit these scenic spots during the Spring Festival
![[fpga] implementation of IIC read / write EEPROM](/img/6a/36e9355058a90d98cffafcbaa1930b.png)
[fpga] implementation of IIC read / write EEPROM

深度学习------不同方法实现Inception-10

Bean creation process and lazy init delay loading mechanism

MySQL查询小工具(一)json格式的字符串字段中,替换json数组中对象的某个属性值

力扣704. 二分查找

Have a heart beating Valentine's day in Singapore

Preorder traversal of Li Kou 589:n fork tree

Unity/ue reads OPC UA and OPC Da data (UE4)

Unity is associated with vs. there is a compiler problem when opening
随机推荐
Have a heart beating Valentine's day in Singapore
Qos(Quality of Service)
Unity realizes rotation and Revolution
brew安装nvm报nvm command not found解决方案
为什么win10开热点后电脑没有网络?
力扣2049:统计最高分的节点数目
Qos(Quality of Service)
Bean creation process and lazy init delay loading mechanism
harbor api 2.0查询
[control] multi agent system summary. 5. system consolidation.
C # Foundation
redis集群概念
What to do when the alicloud SSL certificate expires
Wildcard SSL certificate issuing time
Webots learning notes
Static keyword
Arsenal Stadium Tour - take you to the front and back of Arsenal Stadium
On mask culling of unity
【Paper】2019_ Consensus Control of Multiple AUVs Recovery System Under Switching Topologies and Time D
Royal Albert Hall, a popular landmark in London