当前位置:网站首页>Leetcode skimming ---977
Leetcode skimming ---977
2022-07-03 10:35:00 【Long time no see 0327】
subject : Given a, press 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 .
Input :nums = [-4,-1,0,3,10]
Output :[0,1,9,16,100]
Method 1 : Direct sort
class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
vector<int> ans;
for (int num: nums) {
ans.push_back(num * num);
}
sort(ans.begin(), ans.end());
return ans;
}
};Complexity analysis
Time complexity :O(nlogn)
Spatial complexity :O(logn)
Method 2 : Double pointer
class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
int n = nums.size();
int negative = -1;
for (int i = 0; i < n; ++i) {
if (nums[i] < 0) {
negative = i;
} else {
break;
}
}
vector<int> ans;
int i = negative, j = negative + 1;
while(i >= 0 || j < n) {
if (i < 0) {
ans.push_back(nums[j] * nums[j]);
++j;
} else if (j == n) {
ans.push_back(nums[i] * nums[i]);
--i;
} else if (nums[i] * nums[i] < nums[j] * nums[j]) {
ans.push_back(nums[i] * nums[i]);
--i;
} else {
ans.push_back(nums[j] * nums[j]);
++j;
}
}
return ans;
}
};Complexity analysis
Time complexity :O(n)
Spatial complexity :O(1)
Method 3 : Double pointer
class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
int n = nums.size();
vector<int> ans(n);
for (int i = 0, j = n - 1, pos = n - 1; i <= j; ) {
if (nums[i] * nums[i] > nums[j] * nums[j]) {
ans[pos] = nums[i] * nums[i];
++i;
} else {
ans[pos] = nums[j] * nums[j];
--j;
}
--pos;
}
return ans;
}
};Complexity analysis
Time complexity :O(n)
Spatial complexity :O(1)
边栏推荐
- What useful materials have I learned from when installing QT
- [LZY learning notes -dive into deep learning] math preparation 2.5-2.7
- 深度学习入门之线性回归(PyTorch)
- Leetcode - the k-th element in 703 data flow (design priority queue)
- mysql5.7安装和配置教程(图文超详细版)
- Leetcode刷题---10
- Leetcode刷题---278
- 神经网络入门之预备知识(PyTorch)
- Implementation of "quick start electronic" window dragging
- Hands on deep learning pytorch version exercise solution - 2.3 linear algebra
猜你喜欢

Leetcode - 5 longest palindrome substring

Hands on deep learning pytorch version exercise solution - 2.6 probability

Judging the connectivity of undirected graphs by the method of similar Union and set search

Preliminary knowledge of Neural Network Introduction (pytorch)

Weight decay (pytorch)

一步教你溯源【钓鱼邮件】的IP地址

Tensorflow—Image segmentation

Raspberry pie 4B installs yolov5 to achieve real-time target detection

ECMAScript--》 ES6语法规范 ## Day1

Tensorflow - tensorflow Foundation
随机推荐
Ut2016 learning notes
Data preprocessing - Data Mining 1
SQL Server Management Studio cannot be opened
Content type ‘application/x-www-form-urlencoded; charset=UTF-8‘ not supported
七、MySQL之数据定义语言(二)
Anaconda installation package reported an error packagesnotfounderror: the following packages are not available from current channels:
ECMAScript -- "ES6 syntax specification # Day1
【毕业季】图匮于丰,防俭于逸;治不忘乱,安不忘危。
Leetcode刷题---374
Leetcode刷题---704
实战篇:Oracle 数据库标准版(SE)转换为企业版(EE)
Introduction to deep learning linear algebra (pytorch)
Tensorflow—Neural Style Transfer
Hands on deep learning pytorch version exercise solution - 2.4 calculus
Several problems encountered in installing MySQL under MAC system
What useful materials have I learned from when installing QT
Tensorflow—Image segmentation
一步教你溯源【钓鱼邮件】的IP地址
Multilayer perceptron (pytorch)
Leetcode刷题---75