当前位置:网站首页>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)
边栏推荐
- Leetcode skimming ---10
- 20220601 Mathematics: zero after factorial
- Convolutional neural network (CNN) learning notes (own understanding + own code) - deep learning
- Class-Variant Margin Normalized Softmax Loss for Deep Face Recognition
- Ut2012 learning notes
- Leetcode刷题---367
- [graduation season] the picture is rich, and frugality is easy; Never forget chaos and danger in peace.
- Leetcode刷题---189
- What useful materials have I learned from when installing QT
- Leetcode-106: construct a binary tree according to the sequence of middle and later traversal
猜你喜欢

Free online markdown to write a good resume

Convolutional neural network (CNN) learning notes (own understanding + own code) - deep learning

GAOFAN Weibo app

A super cool background permission management system

mysql5.7安装和配置教程(图文超详细版)

Jetson TX2 brush machine

Softmax 回归(PyTorch)

Powshell's set location: unable to find a solution to the problem of accepting actual parameters

C#项目-寝室管理系统(1)

Out of the box high color background system
随机推荐
Rewrite Boston house price forecast task (using paddlepaddlepaddle)
Codeup: word replacement
波士顿房价预测(TensorFlow2.9实践)
Training effects of different data sets (yolov5)
2-program logic
20220604 Mathematics: square root of X
2018 y7000 upgrade hard disk + migrate and upgrade black apple
Powshell's set location: unable to find a solution to the problem of accepting actual parameters
C#项目-寝室管理系统(1)
Weight decay (pytorch)
Anaconda installation package reported an error packagesnotfounderror: the following packages are not available from current channels:
A complete mall system
What did I read in order to understand the to do list
Advantageous distinctive domain adaptation reading notes (detailed)
Adaptive Propagation Graph Convolutional Network
Jetson TX2 刷机
Leetcode刷题---263
Leetcode-513: find the lower left corner value of the tree
Leetcode刷题---278
Ut2013 learning notes