当前位置:网站首页>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)
边栏推荐
- MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法
- 深度学习入门之线性回归(PyTorch)
- A complete mall system
- Tensorflow—Image segmentation
- Leetcode - 705 design hash set (Design)
- 20220603 Mathematics: pow (x, n)
- 20220606 Mathematics: fraction to decimal
- 3.3 Monte Carlo Methods: case study: Blackjack of Policy Improvement of on- & off-policy Evaluation
- 权重衰退(PyTorch)
- Weight decay (pytorch)
猜你喜欢
I really want to be a girl. The first step of programming is to wear women's clothes
多层感知机(PyTorch)
Hands on deep learning pytorch version exercise solution - 2.4 calculus
Ut2015 learning notes
Leetcode - 1172 plate stack (Design - list + small top pile + stack))
ThreadLocal原理及使用场景
Ind kwf first week
Convolutional neural network (CNN) learning notes (own understanding + own code) - deep learning
2018 Lenovo y7000 black apple external display scheme
Jetson TX2 刷机
随机推荐
神经网络入门之模型选择(PyTorch)
[LZY learning notes -dive into deep learning] math preparation 2.1-2.4
Neural Network Fundamentals (1)
Policy Gradient Methods of Deep Reinforcement Learning (Part Two)
C#项目-寝室管理系统(1)
Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported
Free online markdown to write a good resume
R language classification
What did I read in order to understand the to do list
20220531 Mathematics: Happy numbers
20220606 Mathematics: fraction to decimal
Anaconda installation package reported an error packagesnotfounderror: the following packages are not available from current channels:
Hands on deep learning pytorch version exercise answer - 2.2 preliminary knowledge / data preprocessing
Deep Reinforcement learning with PyTorch
Ind kwf first week
Leetcode刷题---283
一个30岁的测试员无比挣扎的故事,连躺平都是奢望
A super cool background permission management system
EFFICIENT PROBABILISTIC LOGIC REASONING WITH GRAPH NEURAL NETWORKS
深度学习入门之线性回归(PyTorch)