[Preferred algorithm question practice] day1


Double pointer:

1. 283. Moving zero

1. Introduction to the topic

283. Moving Zeros
Given an array nums, write a function that moves all zeros to the end of the array while maintaining the relative order of the non-zero elements.
Note that arrays must be operated on in-place without copying the array.
Insert image description here

2. Problem-solving ideas

3.Code

class Solution {
    
    
public:
    void moveZeroes(vector<int>& nums) {
    
    
        int cur = 0, prev = -1;//cur是0区间后的第一个非0,prev是0区间前的最后一个非0
        while(cur < nums.size())
        {
    
    
            if(nums[cur])
            {
    
    
                swap(nums[++prev], nums[cur]);
            }
            cur++;
        }
    }
};

4. Running results

Insert image description here

2. 1089. Copy zero

1. Introduction to the topic

1089. Copy zeros.
You are given a fixed-length integer array arr. Please copy every zero that appears in the array and shift the remaining elements to the right.
Note: Please do not write elements beyond the length of the array. Please make the above modifications in-place to the input array and do not return anything from the function.
Insert image description here

2. Problem-solving ideas

3.Code

class Solution {
    
    
public:
    void duplicateZeros(vector<int>& arr) {
    
    
        int prev = -1, cur = 0, n = arr.size();//双指针,找到修改后数组的最后一个元素
        while(cur < n)
        {
    
    
            if(arr[cur] != 0)
            {
    
    
                prev++;
            }
            else
            {
    
    
                prev += 2;
            }
            if(prev >= n - 1) break;//prev已经到终止位置
            cur++;
        }
        //处理边界(如果prev超出数组边界2个位置,说明cur判断的最后一个元素是0,因为数组的空间有限,cur最后遍历到的0无法进行复制,因此我们要对这种情况进行修复,即将数组最后一个元素修改为0(直接后移不进行复制)。然后让cur退一步,prev退两步,再进行向后移动元素的操作即可)
        if(prev == n)
        {
    
    
            arr[n - 1] = 0;
            cur--;
            prev -= 2;
        }
        //从最后一个元素开始从后往前移动,遇到0就往后填充两个0.
        while(cur >= 0)
        {
    
    
            if(arr[cur] == 0)
            {
    
    
                arr[prev--] = 0;
                arr[prev--] = 0;
                cur--;
            }
            else
            {
    
    
                arr[prev--] = arr[cur--];
            }
        }
    }
};

4. Running results

Insert image description here

3. 202. Happy Number

1. Introduction to the topic

202. Happy Numbers
Write an algorithm to determine whether a number n is a happy number.
"Happy number" is defined as:
for a positive integer, each time the number is replaced by the sum of the squares of the numbers in each position.
Then repeat this process until the number reaches 1, or it may loop infinitely but never reaches 1.
If the result of this process is 1, then this number is the happy number.
Returns true if n is a happy number; otherwise, returns false.
Insert image description here

2. Problem-solving ideas

3.Code

class Solution {
    
    
public:
    int bitsum(int n)
    {
    
    
        int ret = 0;
        while(n)
        {
    
    
            int t = n%10;
            ret += t * t;
            n /= 10;
        }
        return ret;
    }
    bool isHappy(int n) {
    
    
        int slow = n, fast = bitsum(n);
        while(slow != fast)
        {
    
    
            slow = bitsum(slow);
            fast = bitsum(bitsum(fast));
        }
        return slow == 1;
    }
};

4. Running results

Insert image description here


Summarize

Today is the first day of algorithm practice.
The rope cuts the wood, and the water drops penetrate the stone . Keep working hard.
Source: LeetCode, the copyright belongs to LeetCode.
If this article has inspired you, I hope you can support the author more, thank you all!

Guess you like

Origin blog.csdn.net/xjjxjy_2021/article/details/131503866