当前位置:网站首页>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)
边栏推荐
- Advantageous distinctive domain adaptation reading notes (detailed)
- EFFICIENT PROBABILISTIC LOGIC REASONING WITH GRAPH NEURAL NETWORKS
- Secure in mysql8.0 under Windows_ file_ Priv is null solution
- Leetcode刷题---278
- 深度学习入门之线性代数(PyTorch)
- 七、MySQL之数据定义语言(二)
- 3.3 Monte Carlo Methods: case study: Blackjack of Policy Improvement of on- & off-policy Evaluation
- Deep Reinforcement learning with PyTorch
- 20220605 Mathematics: divide two numbers
- Leetcode - 705 design hash set (Design)
猜你喜欢

EFFICIENT PROBABILISTIC LOGIC REASONING WITH GRAPH NEURAL NETWORKS

Standard library header file

八、MySQL之事务控制语言

Ut2015 learning notes

I really want to be a girl. The first step of programming is to wear women's clothes

An open source OA office automation system

深度学习入门之线性回归(PyTorch)

Hands on deep learning pytorch version exercise solution - 2.6 probability

Knowledge map enhancement recommendation based on joint non sampling learning

Advantageous distinctive domain adaptation reading notes (detailed)
随机推荐
Introduction to deep learning linear algebra (pytorch)
Judging the connectivity of undirected graphs by the method of similar Union and set search
Raspberry pie 4B deploys lnmp+tor and builds a website on dark web
High imitation bosom friend manke comic app
Leetcode刷题---202
High imitation Netease cloud music
Class-Variant Margin Normalized Softmax Loss for Deep Face Recognition
What can I do to exit the current operation and confirm it twice?
八、MySQL之事务控制语言
Seata分布式事务失效,不生效(事务不回滚)的常见场景
Preliminary knowledge of Neural Network Introduction (pytorch)
实战篇:Oracle 数据库标准版(SE)转换为企业版(EE)
Leetcode-513: find the lower left corner value of the tree
Leetcode刷题---832
Hands on deep learning pytorch version exercise solution - 2.6 probability
Hands on deep learning pytorch version exercise solution - 2.3 linear algebra
熵值法求权重
Hands on deep learning pytorch version exercise solution-3.3 simple implementation of linear regression
Leetcode刷题---704
Leetcode-404:左叶子之和