day25

1.剑指 Offer 29. 顺时针打印矩阵

 方形盘龙问题,思路:在数组外面围一堵矩形的墙,碰到墙就掉头

代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define wall -1
 4 #define a 0
 5 int main() {
 6     int cs;
 7     int n,i,j,k=0,b,cnt,next;
 8 
 9     scanf("%d",&cs);
10     while(cs--)
11     {
12 
13         scanf("%d",&n);
14         int s[32][32];
15         for(i=0; i<n+1; i++) {
16             s[0][i]=wall;
17             s[i][0]=wall;
18             s[n+1][i]=wall;
19             s[i][n+1]=wall;
20         }
21         for(i=1; i<=n; i++) {
22             for(j=1; j<=n; j++) {
23                 s[i][j]=a;
24             }
25         }
26         int di[]= {0,1,0,-1};
27         int dj[]= {1,0,-1,0};
28         b=n*n;
29         i=1;
30         j=0;
31         k=0;
32         cnt=0;
33         while(cnt<b) {
34             next=s[i+di[k]][j+dj[k]];
35             if(next==wall||next!=a)
36                 k=(k+1)%4;
37             i=i+di[k];
38             j=j+dj[k];
39             cnt++;
40             s[i][j]=cnt;
41         }
42         for(i=1; i<=n; i++) {
43             for(j=1; j<=n; j++) {
44                 printf("%4d",s[i][j]);
45             }
46             printf("\n");
47         }
48 
49     }
50     return 0;
51 }

 1)顺时针打印矩阵代码(方形盘龙思路):

 1 class Solution {
 2 public:
 3     #define wall -1
 4     int next[4][2] ={0,1,1,0,0,-1,-1,0};//i = i + next[][0],j = j + next[][1]
 5     vector<int> res;
 6     vector<int> spiralOrder(vector<vector<int>>& matrix) {
 7        int m = matrix.size();
 8        if(m == 0) return res;
 9        int n = matrix[0].size();
10        if(n == 0) return res;
11        if(m == 1){
12            for(int col = 0;col < n;col ++)
13             res.push_back(matrix[0][col]);
14            return res;
15        }
16        if(n == 1){
17            for(int row = 0;row < m;row ++)
18             res.push_back(matrix[row][0]);
19            return res;
20        }
21        FirstWall(matrix,m,n);
22        int i = 1,j = 1,k = 0;
23        int cnt = m * n - (2 * (m + n - 2));
24        while(cnt --){
25            if(matrix[i + next[k][0]][j + next[k][1]] == wall){
26                k = (k + 1) % 4;
27            }
28            res.push_back(matrix[i][j]);
29            matrix[i][j] = wall;
30            i = i + next[k][0];
31            j = j + next[k][1];
32        }
33        return res;
34     }
35 
36 private:
37     void FirstWall(vector<vector<int>>& matrix,int m,int n){ //第一层也就是最外面一层“墙”
38         for(int j = 0;j <= n - 2;j ++){
39            res.push_back(matrix[0][j]);printf("%d ",matrix[0][j]);
40            matrix[0][j] = wall;
41        }
42        for(int i = 0;i <= m - 2;i ++){
43            res.push_back(matrix[i][n - 1]);printf("%d ",matrix[i][n - 1]);
44            matrix[i][n - 1] = wall;
45        }
46        for(int j = n - 1;j >= 1;j --){
47            res.push_back(matrix[m - 1][j]);printf("%d ",matrix[m - 1][j]);
48            matrix[m - 1][j] = wall;
49        }
50        for(int i = m - 1;i >= 1;i --){
51            res.push_back(matrix[i][0]);printf("%d ",matrix[i][0]);
52            matrix[i][0] = wall;
53        }
54     }
55 };
t:top,r:right,b:below,l:left
 1 class Solution {
 2 public:
 3     vector<int> spiralOrder(vector<vector<int>>& matrix) {
 4        vector<int> res;
 5        if(matrix.empty())  return res;
 6        int t = 0,r = matrix[0].size() - 1,b = matrix.size() - 1,l = 0;
 7        while(1){
 8         for(int i = l;i <= r;i ++)  res.push_back(matrix[t][i]); //from left to right(the top row)
 9         if(++ t > b) break;
10         for(int i = t;i <= b;i ++)  res.push_back(matrix[i][r]); //from top to below(the right col)
11         if(-- r < l) break;
12         for(int i = r;i >= l;i --)  res.push_back(matrix[b][i]); //from right to left(the below row)
13         if(-- b < t) break;
14         for(int i = b;i >= t;i --)  res.push_back(matrix[i][l]); //from below to top(the left col) 
15         if(++ l > r) break;
16        }
17        return res;
18     }
19 };

2.剑指 Offer 31. 栈的压入、弹出序列

 https://leetcode.cn/leetbook/read/illustration-of-algorithm/5wxgft/
 1 class Solution {
 2 public:
 3     bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
 4        stack<int> stk;
 5        int i = 0;
 6        for(auto x : pushed){
 7            stk.push(x);
 8            while(! stk.empty() && stk.top() == popped[i]){
 9              stk.pop();
10              i ++;
11            }
12        }
13        return stk.empty();
14     }
15 };

 

posted @ 2022-07-21 18:02  balabalahhh  阅读(37)  评论(0)    收藏  举报