Check in (15)

https://leetcode-cn.com/problems/reverse-linked-list-ii/submissions/
This topic is quite simple, you may only need to pay attention to a few small issues.
The head pointer of course always points to the head, but if the head is exchanged later, it is no longer a head pointer. To ensure the accuracy of the head pointer, a head prefix pointer pre should be introduced to indicate the prefix of the head. It is absolutely correct to make the last return pre->next.
And I seem to have a weird obsession with head pointers, logic errors let the errors sink into the water.

class Solution {
    
    
public:
    ListNode* reverseBetween(ListNode* head, int left, int right){
    
    
        int num=right-left;//操作次数
        ListNode* pre,* ending,* temp=new ListNode(0,head),*cur;//前者,尾巴,中间,当前
        ListNode* op=temp;;
        for(int i=0;i<left-1;i++)
            op=op->next;
        pre=op;//找到头前缀

        op=temp;//重新置位
        for(int i=0;i<right;i++)
            op=op->next;//进行寻找交换的尾巴
        ending=op;

        while(num--){
    
    
            cur=pre->next;//确定当前要移动的元素
            pre->next=cur->next;//确定前缀的后继
            cur->next=ending->next;//确定当前元素的后继
            ending->next=cur;//将尾巴的后继修改为当前元素
        }
        return temp->next;
    }
};

https://leetcode-cn.com/problems/spiral-matrix/
This topic seems very interesting. /Eyes open

class Solution {
    
    
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix){
    
    
        int n=matrix.size();
        int m=matrix[0].size();
        vector<vector<int>> matrix_s(n,vector<int>(m,0));
        int sum=0;
        int total=n*m;
        vector<int> ans(total);
        int l=0,r=0;
        while(sum<total){
    
    
            while(r<m&&!matrix_s[l][r]){
    
    
                matrix_s[l][r]=1;
                ans[sum++]=matrix[l][r];
                r++;
            }//向右走
            r--;l++;
            while(l<n&&!matrix_s[l][r]){
    
    
                matrix_s[l][r]=1;
                ans[sum++]=matrix[l][r];
                l++;
            }//向下走
            l--;r--;
            while(r>=0&&!matrix_s[l][r]){
    
    
                matrix_s[l][r]=1;
                ans[sum++]=matrix[l][r];
                r--;
            }//向左走
            l--;r++;
            while(l>=0&&!matrix_s[l][r]){
    
    
                matrix_s[l][r]=1;
                ans[sum++]=matrix[l][r];
                l--;
            }//向上走
            l++;r++;
        }
        return ans;
    }
};

It seems to be a lot of trouble to write in this way. At first, I ignored that the matrix may not be a square matrix. Therefore, when modifying, the modification is always incomplete. At this time, there are always some segmentation errors.

As a digression, I thought I had written a lot of topics, but I thought there were two to three hundred questions. As a result, there are less than one hundred on LeetCode. It is estimated that the total number of topics written in total is no more than 200.
In this case, how big the gap with others would be.

return 0;

Guess you like

Origin blog.csdn.net/weixin_47741017/article/details/114987228