当前位置:网站首页>Leetcode skimming ---75
Leetcode skimming ---75
2022-07-03 10:35:00 【Long time no see 0327】
subject : Give one that contains red 、 White and blue 、 common n Array of elements nums , Sort them in place , Make elements of the same color adjacent , And follow the red 、 white 、 In blue order . We use integers 0、 1 and 2 Red... Respectively 、 White and blue . Must be in a library that does not use sort Function to solve this problem .
Input :nums = [2,0,2,1,1,0]
Output :[0,0,1,1,2,2]
Method 1 : Single pointer
class Solution {
public:
void sortColors(vector<int>& nums) {
int n = nums.size();
int ptr = 0;
for (int i = 0; i < n; ++i) {
if (nums[i] == 0) {
swap(nums[i], nums[ptr]);
++ptr;
}
}
for (int i = ptr; i < n; ++i) {
if (nums[i] == 1) {
swap(nums[i], nums[ptr]);
++ptr;
}
}
}
};Complexity analysis
Time complexity :O(n)
Spatial complexity :O(1)
Method 2 : Double pointer
class Solution {
public:
void sortColors(vector<int>& nums) {
int n = nums.size();
int p0 = 0, p1 = 0;
for (int i = 0; i < n; ++i) {
if (nums[i] == 1) {
swap(nums[i], nums[p1]);
++p1;
} else if (nums[i] == 0) {
swap(nums[i], nums[p0]);
if (p0 < p1) {
swap(nums[i], nums[p1]);
}
++p0;
++p1;
}
}
}
};Complexity analysis
Time complexity :O(n)
Spatial complexity :O(1)
Method 3 : Double pointer two
class Solution {
public:
void sortColors(vector<int>& nums) {
int n = nums.size();
int p0 = 0, p2 = n - 1;
for (int i = 0; i < n; ++i) {
while (i <= p2 && nums[i] == 2) {
swap(nums[i], nums[p2]);
--p2;
}
if (nums[i] == 0) {
swap(nums[i], nums[p0]);
++p0;
}
}
}
};Complexity analysis
Time complexity :O(n)
Spatial complexity :O(1)
边栏推荐
- Leetcode刷题---35
- Ut2014 supplementary learning notes
- [LZY learning notes dive into deep learning] 3.1-3.3 principle and implementation of linear regression
- Timo background management system
- Leetcode刷题---283
- CSDN, I'm coming!
- 20220603 Mathematics: pow (x, n)
- Ut2012 learning notes
- EFFICIENT PROBABILISTIC LOGIC REASONING WITH GRAPH NEURAL NETWORKS
- 2018 y7000 upgrade hard disk + migrate and upgrade black apple
猜你喜欢

2018 Lenovo y7000 black apple external display scheme

Preliminary knowledge of Neural Network Introduction (pytorch)

Tensorflow—Image segmentation

Leetcode - the k-th element in 703 data flow (design priority queue)

Knowledge map enhancement recommendation based on joint non sampling learning

Ut2017 learning notes

Implementation of "quick start electronic" window dragging

A super cool background permission management system

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

Ind wks first week
随机推荐
Yolov5 creates and trains its own data set to realize mask wearing detection
Implementation of "quick start electronic" window dragging
An open source OA office automation system
Ind kwf first week
波士顿房价预测(TensorFlow2.9实践)
Hands on deep learning pytorch version exercise solution - 3.1 linear regression
Ut2014 supplementary learning notes
ECMAScript -- "ES6 syntax specification # Day1
Drop out (pytoch)
Policy gradient Method of Deep Reinforcement learning (Part One)
A complete mall system
Leetcode-404: sum of left leaves
Convolutional neural network (CNN) learning notes (own understanding + own code) - deep learning
[C question set] of Ⅵ
Multi-Task Feature Learning for Knowledge Graph Enhanced Recommendation
Leetcode-100: same tree
Preliminary knowledge of Neural Network Introduction (pytorch)
2018 y7000 upgrade hard disk + migrate and upgrade black apple
Ind FHL first week
权重衰退(PyTorch)