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

力扣LeetBook数组和字符串--二维数组

1.旋转矩阵

题目链接

想了那么久的各种旋转,对角线,其实把问题搞复杂了。

  • 旋转90度的本质无非就是转置+镜像对称
    转置是什么?:将矩阵的行和列互换。
    镜像对称:把矩阵从中间对折,互换位置

矩阵 A

A = [ 1 3 0 2 4 6 ] A = \begin{bmatrix} 1 & 3 & 0 \\ 2 & 4 & 6 \\ \end{bmatrix} A=[123406]

转置后 Aᵀ

A ⊤ = [ 1 2 3 4 0 6 ] A^\top = \begin{bmatrix} 1 & 2 \\ 3 & 4 \\ 0 & 6 \\ \end{bmatrix} A= 130246

class Solution {
public:void rotate(vector<vector<int>>& matrix) {int n=matrix.size();//矩阵的长for(int i=0;i<n;i++)for(int j=i;j<n;j++)swap(matrix[i][j],matrix[j][i]);//转置for(int i=0;i<n;i++)for(int j=0;j<n/2;j++)swap(matrix[i][j],matrix[i][n-j-1]);//镜像对称}
};

Python

class Solution(object):def rotate(self, matrix):""":type matrix: List[List[int]]:rtype: None Do not return anything, modify matrix in-place instead."""n=len(matrix)for i in range(n):#0,n-1for j in range(i,n):#i,n-1matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]  # 直接交换for i in range(n):for j in range(n//2):matrix[i][j], matrix[i][n - j - 1] = matrix[i][n - j - 1], matrix[i][j]

2.零矩阵

题目链接

思路:遍历找出0元素所在行和列的位置,把位置标记出来。
第二次遍历数组,行或列有一个被标记,就把该位置置为0

注意:int flag_row[row]={};错误
row 和 col 虽然是变量,但它们的值是在运行时确定的(比如从 matrix.size() 和 matrix[0].size() 获取),而不是在编译时确定的常量。
在标准 C++ 中,数组的大小必须是编译时常量(比如 constexpr 或 #define 定义的常量)

class Solution {
public:void setZeroes(vector<vector<int>>& matrix) {int row=matrix.size();//行int col=matrix[0].size();//列// int flag_row[row]={};// int flag_col[col]={};std::vector<int> flag_row(row, 0);  // 初始化为全 0std::vector<int> flag_col(col, 0);  // 初始化为全 0for(int i=0;i<row;i++){for(int j=0;j<col;j++){if(matrix[i][j]==0){flag_row[i]=1;flag_col[j]=1;}}}//置为0for(int i=0;i<row;i++){for(int j=0;j<col;j++){if(flag_row[i]||flag_col[j])matrix[i][j]=0;}}}
};

Python版本

class Solution:def setZeroes(self, matrix: List[List[int]]) -> None:"""Do not return anything, modify matrix in-place instead."""row=len(matrix)col=len(matrix[0])flag_row=[0]*rowflag_col=[0]*colfor i in range(row):for j in range(col):if matrix[i][j]==0:flag_row[i]=1flag_col[j]=1for i in range(row):for j in range(col):if flag_row[i]  or flag_col[j]:matrix[i][j]=0
http://www.xdnf.cn/news/879481.html

相关文章:

  • 【无标题】路径着色问题的革命性重构:拓扑色动力学模型下的超越与升华
  • 网络测试实战:金融数据传输的生死时速
  • C++学习-入门到精通【14】标准库算法
  • C++11实现TCP网络通讯服务端处理逻辑简化版
  • ARM处理器工作模式
  • MCP通信方式之Streamable HTTP
  • ZooKeeper 安装教程(Windows + Linux 双平台)
  • Redis 安装配置和性能优化
  • 【bug】Error: /undefinedfilename in (/tmp/ocrmypdf.io.9xfn1e3b/origin.pdf)
  • 仓库拉下ssm项目配置启动
  • Java 高频面试题场景(四):社区老年大学在线学习平台系统
  • Android四大组件通讯指南:Kotlin版组件茶话会
  • 新能源汽车智慧充电桩管理方案:新能源充电桩散热问题及消防安全监管方案
  • 6.5本日总结
  • 【设计模式】门面/外观模式
  • Angular报错:cann‘t bind to ngClass since it is‘t a known property of div
  • Spring Boot 缓存注解详解:@Cacheable、@CachePut、@CacheEvict(超详细实战版)
  • Monorepo架构: Nx Cloud 扩展能力与缓存加速
  • [华为eNSP] OSPF综合实验
  • 在不同型号的手机或平板上后台运行Aidlux
  • 4.3 HarmonyOS NEXT AI驱动的交互创新:智能助手、实时语音与AR/MR开发实战
  • 时序数据库IoTDB的UDF Sample算法在数据监控、故障预防的应用
  • 高并发内存池的轻量级模拟-主体部分:分析拆解多线程内存管理难题
  • Neo4j 完全指南:从入门到精通
  • 文档处理组件Aspose.Words 25.5全新发布 :六大新功能与性能深度优化
  • 《doubao-lite-32k 模型缓存机制使用指南》
  • npm install 相关命令
  • 04-初识css
  • 阿里云ACP云计算备考笔记 (3)——云存储RDS
  • Java转Go日记(六十):gin其他常用知识