当前位置:网站首页>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)
边栏推荐
- Several problems encountered in installing MySQL under MAC system
- Simple real-time gesture recognition based on OpenCV (including code)
- Leetcode刷题---977
- GAOFAN Weibo app
- Leetcode刷题---278
- Leetcode刷题---1385
- Configure opencv in QT Creator
- 20220610 other: Task Scheduler
- Leetcode刷题---10
- 20220602 Mathematics: Excel table column serial number
猜你喜欢
丢弃法Dropout(Pytorch)
Convolutional neural network (CNN) learning notes (own understanding + own code) - deep learning
Ut2014 learning notes
一个30岁的测试员无比挣扎的故事,连躺平都是奢望
MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法
神经网络入门之矩阵计算(Pytorch)
Flutter 退出当前操作二次确认怎么做才更优雅?
GAOFAN Weibo app
Weight decay (pytorch)
Ut2013 learning notes
随机推荐
Powshell's set location: unable to find a solution to the problem of accepting actual parameters
Introduction to deep learning linear algebra (pytorch)
Policy Gradient Methods of Deep Reinforcement Learning (Part Two)
Ind FXL first week
I really want to be a girl. The first step of programming is to wear women's clothes
波士顿房价预测(TensorFlow2.9实践)
Tensorflow—Image segmentation
ECMAScript--》 ES6语法规范 ## Day1
20220607 others: sum of two integers
Leetcode - 1172 plate stack (Design - list + small top pile + stack))
Implementation of "quick start electronic" window dragging
Free online markdown to write a good resume
Knowledge map enhancement recommendation based on joint non sampling learning
Codeup: word replacement
20220609 other: most elements
Leetcode - 706 design hash mapping (Design)*
Leetcode刷题---217
Hands on deep learning pytorch version exercise answer - 2.2 preliminary knowledge / data preprocessing
LeetCode - 900. RLE iterator
Jetson TX2 刷机