题目: 2326. Spiral matrix IV
给你一个Link list,指定整数m,n请将Link list中的值从0,0的位置开始顺时针螺旋的
填入m*n的2D vector中,不足者全填-1
思路:
照做,可以先用两组vector纪录x,y的移动惯例方向,当填完数字后看更新后的x,y loc
有没有超出0-n-1, 0-m-1的范围或是碰到ans[y][x]!=-1的格子,如果有的话则复原重新
换方向更新,beat70%以上有蛮多新做法来完成这个动作,可以多参考
vector<vector<int>> spiralMatrix(int m, int n, ListNode* head) {
        vector<vector<int>> ans(m,vector<int>(n,-1));
        int nx=0;
        int ny=0;
        vector<int> dirx={1,0,-1,0};
        vector<int> diry={0,1,0,-1};
        int topc=0;
        while(head){
            ans[ny][nx]=head->val;
            head=head->next;
            nx+=dirx[topc%4];
            ny+=diry[topc%4];
            if(nx<0 || nx>n-1 || ny<0 || ny>m-1 || ans[ny][nx]!=-1){
                nx-=dirx[topc%4];
                ny-=diry[topc%4];
                ++topc;
                nx+=dirx[topc%4];
                ny+=diry[topc%4];
            }
        }
        return ans;
    }