首页 > 其他分享 >二维数组花式遍历(旋转,螺旋) [labuladong-刷题打卡 day5]

二维数组花式遍历(旋转,螺旋) [labuladong-刷题打卡 day5]

时间:2023-08-05 11:56:06浏览次数:41  
标签:vector matrix int day5 directionIndex 遍历 打卡 labuladong row

矩阵旋转

48. 旋转图像
难点主要在于:

  1. 用翻转和镜像处理逆反和旋转,和逆转单词一样“难者不会,会者不难”,思路简单
  2. 镜像的坐标对应关系处理
  3. 语言特性的利用,不同语言有不同api,实际代码中会有很大不同,但思想一致
  4. 如果确定矩阵维数,通过线性代数应该可以直接计算答案...
class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        //先行逆转
        for(auto& row :matrix){
            reverse(row.begin(),row.end());
        }
        int n=matrix[0].size();
        //副对角线镜像
        //matrix[i][j] <=> matrix[n-j-1][n-i-1]
        //对角线上元素不用镜像,所以n-1
        for(int i=0;i<n-1;i++){
            for(int j=0;j<n-i-1;j++){
                swap(matrix[i][j],matrix[n-j-1][n-i-1]);
            }
        }
    }
};

螺旋矩阵

54. 螺旋矩阵
螺旋遍历其实也可以看作是一种迭代,每次遍历最外层,下一次遍历Matrix[m-1][n-1]的最外层,但实际执行时更重要的是遍历方向的处理,主要有两种思路:

  1. 一种有点像图形学中设置方向向量,在四种方向中碰壁后循环下一个方向向量
  2. 第二种就是不断迭代,每次迭代中处理四个方向,需要嵌套循环
    最主要的就还是“碰壁”的判断,两种方法不一样,第一种设置访问矩阵确定是否是已访问数组,第二种则是边界判断
    这里我直接贴第一种 模拟法 感觉比较有趣
class Solution {
private:
    static constexpr int directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        if (matrix.size() == 0 || matrix[0].size() == 0) {
            return {};
        }
        
        int rows = matrix.size(), columns = matrix[0].size();
        vector<vector<bool>> visited(rows, vector<bool>(columns));//访问标记数组
        int total = rows * columns;//总路径长度
        vector<int> order(total);//结果存储

        int row = 0, column = 0;
        int directionIndex = 0;
        for (int i = 0; i < total; i++) {
            //访问
            order[i] = matrix[row][column];
            visited[row][column] = true;
            //计算前进方向
            int nextRow = row + directions[directionIndex][0];
            int nextColumn = column + directions[directionIndex][1];
            //判断越界和重复访问
            if (nextRow < 0 || nextRow >= rows || nextColumn < 0 || nextColumn >= columns || visited[nextRow][nextColumn]) {
                directionIndex = (directionIndex + 1) % 4;
            }
            row += directions[directionIndex][0];
            column += directions[directionIndex][1];
        }
        return order;
    }
};

标签:vector,matrix,int,day5,directionIndex,遍历,打卡,labuladong,row
From: https://www.cnblogs.com/alanjiang/p/17607727.html

相关文章

  • vue--day54--todolist 中的MyItem 和App 消息发布实现通信
    1.App.vue<template><divid="root"><divclass="todo-container"><divclass="todo-wrap"><!--@addTodo事件名addTodo回调名--><MyHeader@addTodo="addTodo"/><!--父亲给儿子传数据父亲通过数据绑定......
  • 8.4打卡
    L2-010排座位#include<iostream>usingnamespacestd;intN,M,K,a[101][101]={0},fri[101]={0},p1,p2,ship;intisFriend(intx){ returnfri[x]==x?x:fri[x]=isFriend(fri[x]);}intmain(){ cin>>N>>M>>K; for(in......
  • vue--day51----todolist 中App.vue 和 MyItem.vue 类似 父亲和子通信 通过全局事件总
    1.mainjs/***该文件是整个项目的入口文件*///引入VueimportVuefrom'vue'//引入App组件他是所有组件的父组件importAppfrom'./App.vue'//关闭vue的生产提示Vue.config.productionTip=false//constDemo=Vue.extend({})//constd=newDemo();//Vue.pr......
  • 8.3打卡
    L2-007家庭房产#include<cstdio>#include<algorithm>#include<vector>#include<map>#include<set>usingnamespacestd;structfam{ intid,num; doubleavg1,avg2;};constintN=10000;intfather[N];introot[N];boolcmp(structfama,......
  • Week6 Day5
    偶吼吼 今天终于来到了 图形用户接口 终于能接触到有关设计之类的东西了  GUI从创建window开始通常会使用JFrameJFrameframe=newJFrame();可以这样加入按钮、文字字段等: frame.getContentPane().add(button);你得指定尺寸和执行显示动作:frame.setSize(300,300......
  • 数组双指针技巧汇总 [labuladong-刷题打卡 day2]
    https://labuladong.github.io/algo/challenge/ji-chu-tiao-zhan/day02/快慢指针26.删除有序数组中的重复项两个指针分别维护符合条件数组和待删除数组,当快指针移动时将符合条件元素插入已完成数组后即可。通过这两天对双指针的练习,可以发现很多双指针算法其实也是一种迭代算......
  • 前缀和数组技巧 [labuladong-刷题打卡 day3]
    今天是两道前缀和,主要有一维前缀和和二维前缀和,当然扩充到高维也是可以的,只不过状态转移会相对复杂些。这里直接贴一个动态规划的介绍吧:动态规划要素动态规划概念、特点、经典例题和于其它算法思想的比较前缀和其实是备忘录自底向上动态规划算法的一个典型例子,状态转移方程:一......
  • Python基础day57 Django模板继承和模型层
    模板之标签就是在模板里面使用流程控制:if、else、elseif、for标签看起来是这样的:{%tag%}for标签{%forpersoninperson_list%}{{forloop}}<p>{{person.name}}</p>{%endfor%}{%forpersoninperson_list%}{#判断list是否有值,没有就走empty#}......
  • vue--day50--todolist案例自定义事件修改footer 和header 修改
    1.MyHeader.vue<template><divclass="todo-header"><!--v-model:="title"是实时绑定的--><inputtype="text"placeholder="请输入你的任务名称,回车键确认"v-model="title"@keyup.enter="add"/>......
  • 8.1打卡
    L1-087机工士姆斯塔迪奥#include<bits/stdc++.h>usingnamespacestd;intmain(){intN,M,Q;cin>>N>>M>>Q;intsum=0;inta[N][M];memset(a,0,sizeof(a));for(intk=1;k<=Q;k++){intT,C;cin>>T>>C......