当前位置:网站首页>Leetcode skimming ---283
Leetcode skimming ---283
2022-07-03 10:35:00 【Long time no see 0327】
subject : Given an array nums, Write a function that will 0 Move to end of array , While maintaining the relative order of non-zero elements . Please note that , You must operate on the array in place without copying it .
Input :nums = [0,1,0,3,12]
Output :[1, 3, 12, 0, 0]
Method 1 : Double pointer
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int n = nums.size(), left = 0, right = 0;
while (right < n) {
if (nums[right]) {
swap(nums[left], nums[right]);
left++;
}
right++;
}
}
};
Complexity analysis
Time complexity :O(n)
Spatial complexity :O(1)
边栏推荐
- Hands on deep learning pytorch version exercise solution - 2.3 linear algebra
- Ind FXL first week
- Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported
- 3.3 Monte Carlo Methods: case study: Blackjack of Policy Improvement of on- & off-policy Evaluation
- 2018 Lenovo y7000 black apple external display scheme
- 深度学习入门之线性回归(PyTorch)
- Ut2013 learning notes
- Leetcode刷题---35
- Anaconda installation package reported an error packagesnotfounderror: the following packages are not available from current channels:
- Standard library header file
猜你喜欢
Anaconda installation package reported an error packagesnotfounderror: the following packages are not available from current channels:
一步教你溯源【钓鱼邮件】的IP地址
Knowledge map enhancement recommendation based on joint non sampling learning
神经网络入门之预备知识(PyTorch)
Hands on deep learning pytorch version exercise solution - 2.3 linear algebra
Tensorflow—Image segmentation
Leetcode-513: find the lower left corner value of the tree
Leetcode - 706 design hash mapping (Design)*
Tensorflow - tensorflow Foundation
Handwritten digit recognition: CNN alexnet
随机推荐
【毕业季】图匮于丰,防俭于逸;治不忘乱,安不忘危。
Introduction to deep learning linear algebra (pytorch)
二分查找法
丢弃法Dropout(Pytorch)
20220605 Mathematics: divide two numbers
Ut2014 supplementary learning notes
Ind wks first week
Leetcode刷题---704
Leetcode刷题---1
波士顿房价预测(TensorFlow2.9实践)
Leetcode刷题---75
[LZY learning notes -dive into deep learning] math preparation 2.1-2.4
ECMAScript -- "ES6 syntax specification # Day1
Label Semantic Aware Pre-training for Few-shot Text Classification
[C question set] of Ⅵ
Weight decay (pytorch)
Leetcode刷题---35
Raspberry pie 4B deploys lnmp+tor and builds a website on dark web
Jetson TX2 刷机
Leetcode刷题---202