当前位置:网站首页>【C#】重塑矩阵(交错数组)
【C#】重塑矩阵(交错数组)
2022-04-21 06:34:00 【阿月浑子2021】
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/reshape-the-matrix
给一个由二维数组 mat 表示的 m x n 矩阵,以及两个正整数 r 和 c ,分别表示想要的重构的矩阵的行数和列数。
重构后的矩阵需要将原始矩阵的所有元素以相同的 行遍历顺序 填充。
如果具有给定参数的 reshape 操作是可行且合理的,则输出新的重塑矩阵;否则,输出原始矩阵。

m == mat.length
n == mat[i].length
1 <= m, n <= 100
-1000 <= mat[i][j] <= 1000
1 <= r, c <= 300
public class Solution {
public int[][] MatrixReshape(int[][] mat, int r, int c) {
int m=mat.Length;
int n=mat[0].Length;
if(m*n!=r*c) return mat;
Queue<int> queue=new Queue<int>();
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
queue.Enqueue(mat[i][j]);
}
}
int[][] result=new int[r][];
for(i=0;i<r;i++)
{
result[i]=new int[c];//交错数组实例化
for(j=0;j<c;j++)
{
if(queue.Count!=0)
result[i][j]=queue.Dequeue();
}
}
return result;
}
}
还可利用整除取余来表示新矩阵的行列(看起来高级,但挺耗时的):
for(i=0;i<r;i++)
{
result[i]=new int[c];
}
for(i=0;i<m*n;i++)
{
result[i/c][i%c]=mat[i/n][i%n];
}

附加一个[3,3,9]的三维数组:
int[,,] boxes = {
{
{ 0,1,2,3,4,5,6,7,8,9},
{ 0,1,2,3,4,5,6,7,8,9},
{ 0,1,2,3,4,5,6,7,8,9}
} ,
{
{ 0,1,2,3,4,5,6,7,8,9},
{ 0,1,2,3,4,5,6,7,8,9},
{ 0,1,2,3,4,5,6,7,8,9}
},
{
{ 0,1,2,3,4,5,6,7,8,9},
{ 0,1,2,3,4,5,6,7,8,9},
{ 0,1,2,3,4,5,6,7,8,9}
}
};
版权声明
本文为[阿月浑子2021]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_56814032/article/details/121873247
边栏推荐
- Jichuang north, chipone, icn6211, icn6202, Mipi to RGB, Mipi to LVDS, maximum resolution 1080p, Mipi DSI conversion, Mipi conversion,
- ConvergenceWarning: Liblinear failed to converge, increase the number of iterations解决辦法
- 【WPF】Popup
- Blue Bridge Cup -- sequence sequencing problem
- Macro crystal micro ms9288, analog RGB (VGA) to HDMI, HDMI transmitter, audio decoder, built-in MCU
- 音频功率放大器,纳芯威功放,NS4165B单通道5.3W AB\D类功放,主要替换智浦芯的CS8571、CS8871、CS5218海栎创的HAA2018
- Nmap scanning and scapy projects
- Knowledge of classes and objects
- PIM-DM
- Cs5518, Mipi to dual LVDS, replacement: gm8775 of Guoteng, tc358775 of Toshiba, dual LVDS, domestic perfect replacement, DSI to dual channel LVDS, LVDS clock frequency up to 154mhz, 1920 x1200
猜你喜欢

NP、OSPF路由聚合

有关imap-tools模块实现邮件移动的问题

Macro crystal micro ms9123, USB screen projection control chip, USB screen projector, support CVBS, S-Video video interface,

Cs5801 specification | cs5801 HDMI to EDP conversion scheme | HDMI to DP adapter board design, hdmi2 0 turn edp1 4. Support downward compatibility

NP, NSSA area

Cjc5340, ADC, DAC, replacement cs5340, digital to analog converter, 100db192khz multi bit audio A / D converter, audio codec100db192khz multi bit audio A / D converter

【无标题】国腾GM系列,GM8284DD(GM8284DR,LT8218A)、 GM8285C、GM7123C,LVDSTTL转TTL,TTL转成单路LVDS,TTL数字信号转换成VGA

Sort method (2) = > insert sort

宏晶微MS9288、模拟RGB(VGA)转HDMI,HDMI发射器,音频解码器,内置MCU

Using Wireshark to restore pcap data stream to picture file
随机推荐
Longxun series: lt8911, transfer chip for converting LVDS / Mipi DSI signal into EDP signal, lt8619, HDMI / MHL signal into LVDS / RGB signal
Blue Bridge Cup - A + B problem
Blind guess account password
2020-12-24
排序方式(2)==>插入排序
2020-12-24
Click a tag to jump to the div block of the specified ID content
蓝桥杯——A+B问题
Cjc5340,ADC,DAC,替换cs5340,数模转换器,100dB192KHz多位音频A/D转换器,音频Codec100dB192KHz多位音频A/D转换器
Longxun series: lt8912b, lt6911c, HDMI to Mipi (CSI \ DSI), single channel mipi2 DSi bridge to LVDS / HDM, hdmi1 4 to dual port mipidsi/csi and audio data sheet
NP, NSSA area
类和对象的相关知识
迷你考试系统v1.0.0版本
什么是WebSocket
音频功率放大器,纳芯威功放,NS4165B单通道5.3W AB\D类功放,主要替换智浦芯的CS8571、CS8871、CS5218海栎创的HAA2018
Common MySQL statements
集创北方,Chipone,ICN6211,ICN6202,MIPI转RGB,MIPI转LVDS,分辨率最高1080P,MIPI DSI转换,mipi转换,
Basic concepts of IP Multicast
Blue Bridge Cup - hex to octal
蓝桥杯——十六进制与十进制之间的转换