移动零 - 简单

*************

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 = 0right = 0

分步执行

  1. Step 1(right=0)

    • 检测元素‌:nums[right] = 0
    • 操作‌:跳过交换,right 右移至 1
    • 当前数组‌:[0, 1, 0, 3, 12]
      left=0right=1
  2. Step 2(right=1)

    • 检测元素‌:nums[right] = 1(非零)
    • 交换操作‌:
      • 交换 nums[left](0)与 nums[right](1)‌
      • left 右移至 1
    • 交换后数组‌:[1, 0, 0, 3, 12]
      left=1right=2
  3. Step 3(right=2)

    • 检测元素‌:nums[right] = 0
    • 操作‌:跳过交换,right 右移至 3
    • 当前数组‌:[1, 0, 0, 3, 12]
      left=1right=3
  4. Step 4(right=3)

    • 检测元素‌:nums[right] = 3(非零)
    • 交换操作‌:
      • 交换 nums[left](0)与 nums[right](3)‌
      • left 右移至 2
    • 交换后数组‌:[1, 3, 0, 0, 12]
      left=2right=4
  5. Step 5(right=4)

    • 检测元素‌:nums[right] = 12(非零)
    • 交换操作‌:
      • 交换 nums[left](0)与 nums[right](12)‌
      • left 右移至 3
    • 最终数组‌:[1, 3, 12, 0, 0]
      left=3right=5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值