当前位置:网站首页>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)
边栏推荐
- Softmax regression (pytorch)
- Secure in mysql8.0 under Windows_ file_ Priv is null solution
- QT creator uses OpenCV Pro add
- Powshell's set location: unable to find a solution to the problem of accepting actual parameters
- Leetcode skimming ---44
- ECMAScript--》 ES6语法规范 ## Day1
- 20220605 Mathematics: divide two numbers
- Ut2013 learning notes
- Yolov5 creates and trains its own data set to realize mask wearing detection
- 20220609 other: most elements
猜你喜欢
Hands on deep learning pytorch version exercise solution - 2.3 linear algebra
mysql5.7安装和配置教程(图文超详细版)
Install yolov3 (Anaconda)
A super cool background permission management system
熵值法求权重
Hands on deep learning pytorch version exercise solution - 2.4 calculus
Leetcode - the k-th element in 703 data flow (design priority queue)
Implementation of "quick start electronic" window dragging
[C question set] of Ⅵ
Judging the connectivity of undirected graphs by the method of similar Union and set search
随机推荐
A complete mall system
Linear regression of introduction to deep learning (pytorch)
Content type ‘application/x-www-form-urlencoded; charset=UTF-8‘ not supported
[LZY learning notes -dive into deep learning] math preparation 2.1-2.4
Introduction to deep learning linear algebra (pytorch)
conda9.0+py2.7+tensorflow1.8.0
Boston house price forecast (tensorflow2.9 practice)
Hands on deep learning pytorch version exercise solution - 2.6 probability
Data classification: support vector machine
Neural Network Fundamentals (1)
Leetcode skimming ---263
MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法
Drop out (pytoch)
Handwritten digit recognition: CNN alexnet
Numpy Foundation
Training effects of different data sets (yolov5)
Preliminary knowledge of Neural Network Introduction (pytorch)
深度学习入门之线性代数(PyTorch)
Seata分布式事务失效,不生效(事务不回滚)的常见场景
Install yolov3 (Anaconda)