*************
C++
topic: 283. 移动零 - 力扣(LeetCode)
*************
In the past few days, I did the shadowing reading practice to be a native speaker. It is really annoyiny that the hoster speeks too fast and I cannot keep up with the speed. One thing that really upset is that I donot do the practices a few days later. It seems working and then I give up. I realize that it is not good habbit and need to change it. After a half-year self-learning c++, it is the time to relearn the code to find something new.
编程基础 0 到 1 - 学习计划 - 力扣(LeetCode)全球极客挚爱的技术成长平台https://leetcode.cn/studyplan/programming-skills/
OK, have a glance at todays topic:
![]() |
This time I want to write the code with personal style. Good habbit maks every steps.
If nums[1] = 0, nums[2] = 1, swap the nums. nums[1] = 1, nums[2] = 0. This is the easy-understand way to line the 0 elements in the end of the strings. If muns[right] != 0, swap the left element and the right element.
class Solution {
public:
void moveZeroes(vector<int>& nums) {
private:
public :
if (nums[right] != 0)
{
swap(nums[left], nums[right]);
}
}
};
the grammar of the swap is easy.
// swap 是通过三次赋值实现:
void swap(T& a, T& b)
{
T temp = a;
a = b;
b = temp;
}
then move the pointer right.
class Solution {
public:
void moveZeroes(vector<int>& nums) {
private:
public :
if (nums[right] != 0)
{
// 交换左右元素,确保右边的元素为0
swap(nums[left], nums[right]);
++left;
}
}
};
make sure that right and left strat from 0, so the checking will travel all the elements.
class Solution {
public:
void moveZeroes(vector<int>& nums) {
private:
public :
if (nums[right] != 0)
{
for (right = 0; right < n; ++right)
{
// 交换左右元素,确保右边的元素为0
swap(nums[left], nums[right]);
++left;
}
}
}
};
and then declare the variables.
class Solution {
public:
void moveZeroes(vector<int>& nums) {
private:
int left = 0;
int right = 0;
int n = nums.size();
public :
if (nums[right] != 0)
{
for (right = 0; right < n; ++right)
{
// 交换左右元素,确保右边的元素为0
swap(nums[left], nums[right]);
++left;
}
}
}
};
In the fact, the code is wrong even if the steps is right. Because it is already have a class.
![]() |
so in the class Solution, donot need a private to decalare the variables any more.
class Solution {
public:
void moveZeroes(vector<int>& nums) {
//private:
int left = 0;
//int right = 0;
int n = nums.size();
//public :
{
for (right = 0; right < n; ++right)
{
if (nums[right] != 0)
{
// 交换左右元素,确保右边的元素为0
swap(nums[left], nums[right]);
++left;
}
}
}
}
};
Even if the code above is wrong. The element right is not declared.
class Solution {
public:
void moveZeroes(vector<int>& nums) {
//private:
int left = 0;
//int right = 0;
int n = nums.size();
//public :
{
// 这里要定义 right
for (int right = 0; right < n; ++right)
{
if (nums[right] != 0)
{
// 交换左右元素,确保右边的元素为0
swap(nums[left], nums[right]);
++left;
}
}
}
}
};
Here annotate the int right = 0 in the line 7 is necessary. otherwise the int right is always 0.
the code start from line 1 to the line end. int right changes should happen in the if loop, which makes code runs well.
class Solution {
public:
void moveZeroes(vector<int>& nums) {
// declare the the varables
int left = 0;
int n = nums.size();
// the main code
for (int right = 0; right < n; ++right)
{
if (nums[right] != 0)
{
// 交换左右元素,确保右边的元素为0
swap(nums[left], nums[right]);
++left;
}
}
}
};
debug the code is a really funny thing in vscode, but here cannot present the process of the debug. So list the process of the running.
初始状态
- 数组:
[0, 1, 0, 3, 12]
- 指针位置:
left = 0
,right = 0
分步执行
-
Step 1(right=0)
- 检测元素:
nums[right] = 0
- 操作:跳过交换,
right
右移至1
- 当前数组:
[0, 1, 0, 3, 12]
(left=0
,right=1
)
- 检测元素:
-
Step 2(right=1)
- 检测元素:
nums[right] = 1
(非零) - 交换操作:
- 交换
nums[left]
(0)与nums[right]
(1) left
右移至1
- 交换
- 交换后数组:
[1, 0, 0, 3, 12]
(left=1
,right=2
)
- 检测元素:
-
Step 3(right=2)
- 检测元素:
nums[right] = 0
- 操作:跳过交换,
right
右移至3
- 当前数组:
[1, 0, 0, 3, 12]
(left=1
,right=3
)
- 检测元素:
-
Step 4(right=3)
- 检测元素:
nums[right] = 3
(非零) - 交换操作:
- 交换
nums[left]
(0)与nums[right]
(3) left
右移至2
- 交换
- 交换后数组:
[1, 3, 0, 0, 12]
(left=2
,right=4
)
- 检测元素:
-
Step 5(right=4)
- 检测元素:
nums[right] = 12
(非零) - 交换操作:
- 交换
nums[left]
(0)与nums[right]
(12) left
右移至3
- 交换
- 最终数组:
[1, 3, 12, 0, 0]
(left=3
,right=5
)
- 检测元素: