当前位置: 首页 > news >正文

Day7--HOT100--54. 螺旋矩阵,48. 旋转图像,240. 搜索二维矩阵 II

Day7–HOT100–54. 螺旋矩阵,48. 旋转图像,240. 搜索二维矩阵 II

每日刷题系列。今天的题目是力扣HOT100题单。

题目类型:矩阵。(方法:模拟,标记,翻转,排除法)

54. 螺旋矩阵

方法:模拟

思路【我】:

做过几遍,背下来了……

每次要保持循环不变量,就是每次遍历的区间都是左闭右闭。

先遍历上边和右边,然后判断有没有越界,再遍历下边和左边。不然会重复遍历。

class Solution {public List<Integer> spiralOrder(int[][] matrix) {List<Integer> res = new ArrayList<>();int n = matrix.length;int m = matrix[0].length;int left = 0;int right = m - 1;int top = 0;int bottom = n - 1;while (left <= right && top <= bottom) {for (int i = left; i <= right; i++) {res.add(matrix[top][i]);}top++;for (int i = top; i <= bottom; i++) {res.add(matrix[i][right]);}right--;// 左闭右闭的话,这里要再判一次if (left <= right && top <= bottom) {for (int i = right; i >= left; i--) {res.add(matrix[bottom][i]);}bottom--;for (int i = bottom; i >= top; i--) {res.add(matrix[i][left]);}left++;}}return res;}
}

思路【@灵艾山茶府】:

1,自定义方向标dirs

2,加入res后标记走过的地方

3,预测下一步能不能走,不能走的话,右转90度

class Solution {private static final int[][] dirs = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; // 右下左上public List<Integer> spiralOrder(int[][] matrix) {int n = matrix.length;int m = matrix[0].length;List<Integer> res = new ArrayList<>();int i = 0;int j = 0;int di = 0;for (int k = 0; k < n * m; k++) {// 1,加入res后标记res.add(matrix[i][j]);matrix[i][j] = Integer.MAX_VALUE;// 2,预测下一步看看能不能走int x = i + dirs[di][0];int y = j + dirs[di][1];// 2.1,不能走。如果 (x, y) 出界或者已经访问过if (x < 0 || x >= n || y < 0 || y >= m || matrix[x][y] == Integer.MAX_VALUE) {// 右转 90°di = (di + 1) % 4;}// 2.2,能走。(这里不能等于(x,y)!)i += dirs[di][0];j += dirs[di][1];}return res;}
}

48. 旋转图像

思路【@灵艾山茶府】:

用翻转操作代替旋转操作。

旋转90度:(i,j)→(j,n−1−i)

两次翻转:(i,j)转置(j,i)行翻转(j,n−1−i)

1,转置。(遍历对角线下方元素)

2,行翻转。

class Solution {public void rotate(int[][] matrix) {int n = matrix.length;int m = matrix[0].length;// 1,转置。(遍历对角线下方元素)for (int i = 0; i < n; i++) {for (int j = 0; j < i; j++) {int temp = matrix[i][j];matrix[i][j] = matrix[j][i];matrix[j][i] = temp;}}// 2,行翻转。for (int i = 0; i < n; i++) {for (int j = 0; j < m / 2; j++) {int temp = matrix[i][j];matrix[i][j] = matrix[i][n - 1 - j];matrix[i][n - 1 - j] = temp;}}return;}
}

240. 搜索二维矩阵 II

思路【@灵艾山茶府】:

利用 matrix 行列有序的性质,我们可以用 O(1) 的时间获取 O(m) 或 O(n) 的信息。相比之下,O(mn) 的暴力查找(一个一个地找),每次花费 O(1) 的时间,只获取了 O(1) 的信息。

class Solution {public boolean searchMatrix(int[][] matrix, int target) {int n = matrix.length;int m = matrix[0].length;// 从右上角开始查找int i = 0;int j = m - 1;while (i < n && j >= 0) {// 找到了,返回if (matrix[i][j] == target) {return true;}// 如果右上角的元素比target小,那么它这一行的元素都排除(因为它是这一行里面最大的)if (matrix[i][j] < target) {i++;} else {// 如果右上角的元素比target大,那么它这一列的元素都排除(因为它是这一列里面最小的)j--;}}return false;}
}
http://www.xdnf.cn/news/1377955.html

相关文章:

  • 【JAVA实现websocket】
  • Java设计模式之《外观模式》
  • 大模型安全概述、LlamaFirewall
  • 深度学习---卷积神经网络CNN
  • Git-远程操作
  • AI-Agent 深度科普:从概念到架构、应用与未来趋势
  • JVM之【Java对象在内存中的结构】
  • Linux--->网络编程(TCP并发服务器构建:[ 多进程、多线程、select ])
  • Linux 系统调优与CPU-IO-网络内核参数调优
  • MySQL InnoDB vs MyISAM
  • 深度学习——卷积神经网络CNN(原理:基本结构流程、卷积层、池化层、全连接层等)
  • LeetCode - 反转链表 / K 个一组翻转链表
  • day2_softmax回归的实现 李沐动手学深度学习pytorch记录
  • 神经网络学习笔记12——高效卷积神经网络架构MobileNet
  • PLC_博图系列☞基本指令”S_ODT:分配接通延时定时器参数并启动“
  • leecode-三数之和
  • 如何防御安全标识符 (SID) 历史记录注入
  • 【Linux实时内核机制】ww_rt_mutex 的contending_lock异常问题
  • wireshark解析FLV插件分享
  • Unity Shader unity文档学习笔记(二十一):几种草体的实现方式(透明度剔除,GPU Instaning, 曲面细分+几何着色器实现)
  • HTML5超详细学习内容
  • GPIO推挽和开漏的名称由来和本质含义
  • FactoryBean接口作用
  • 使用Stone 3D快速制作第一人称视角在线小游戏
  • 【PyTorch】基于YOLO的多目标检测项目(二)
  • 基于Cursor AI IDE的Vue3留言板系统实战:从零搭建到智能优化全流程
  • 《金融对账系统雪崩隐患的深度复盘与架构重生》
  • 从CTFshow-pwn入门-pwn40理解64位栈溢出不都需要堆栈平衡
  • 致远OA新闻公告讨论调查信息查询SQL
  • Linux操作系统——TCP服务端并发模型