https://leetcode.com/problems/reshape-the-matrix/description/

public int[][] matrixReshape(int[][] nums, int r, int c) {
int m = nums.length, n = nums[].length;
if (r * c != m * n)
return nums;
int[][] reshaped = new int[r][c];
for (int i = ; i < r * c; i++)
reshaped[i/c][i%c] = nums[i/n][i%n]; %核心公式
return reshaped;
}

序号对行数整除,取整是所在行数。对行数求余,是所在列数。

自己的代码:1.不够简洁 ,有点累赘 2.compiler error,找不出原因,哈哈哈哈

class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
vector<vector<int>> nums1(r,vector<int>(c));
if((nums.size()*nums[].size())==r*c)
{ for(int i=;i<r;i++)
{
for(int j=;j<c;j++)
{
cout<<nums[i][j]<<" ";
nums1[i][j]=nums[i][j]; }
cout<<"\n";
} }
else
{
cout<<"the shape no match!"<<endl;
for(int i=;i<nums.size();i++)
{
for(int j=;j<nums[i].size();j++)
{
cout<<nums[i][j]<<" ";
nums1[i][j]=nums[i][j]; }
cout<<"\n";
} } return nums1;
}};

leetcode566. Reshape the Matrix的更多相关文章

  1. Leetcode566.Reshape the Matrix重塑矩阵

    在MATLAB中,有一个非常有用的函数 reshape,它可以将一个矩阵重塑为另一个大小不同的新矩阵,但保留其原始数据. 给出一个由二维数组表示的矩阵,以及两个正整数r和c,分别表示想要的重构的矩阵的 ...

  2. LeetCode 566. 重塑矩阵(Reshape the Matrix)

    566. 重塑矩阵 566. Reshape the Matrix 题目描述 LeetCode LeetCode LeetCode566. Reshape the Matrix简单 Java 实现 c ...

  3. [Swift]LeetCode566. 重塑矩阵 | Reshape the Matrix

    In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...

  4. Reshape the Matrix

    In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...

  5. LeetCode 566. Reshape the Matrix (重塑矩阵)

    In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...

  6. leetcode算法:Reshape the Matrix

    In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...

  7. [LeetCode] Reshape the Matrix 重塑矩阵

    In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...

  8. 566. Reshape the Matrix

    In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...

  9. LeetCode 566 Reshape the Matrix 解题报告

    题目要求 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ...

随机推荐

  1. java工程师_基础_阶段一_HTML笔记篇

    一.了解HTML语言 html:超文本标记语言. 二.HTML整体结构<html> <head> </head> <body> </body> ...

  2. 【java错误】错误: 编码GBK的不可映射字符

    java源代码 今天在写java是出现一个编码错误,这里先将书上的java源代码贴出来. import java.io.Console; public class ConsoleTest { //用j ...

  3. BS和CS的区别

    C/S又称Client/Server或客户/服务器模式.服务器通常采用高性能的PC.工作站或小型机,并采用大型数据库系统,如Oracle.Sybase.Informix或 SQL Server.客户端 ...

  4. 关系型数据库基本概念及MySQL简述

    数据库基本概念">关系型数据库基本概念 数据库: 对大量信息进行管理的高效解决方案. 按照数据结构来组织.存储和管理数据的库. 数据库系统(DBS,DATABASE SYSTEM): ...

  5. Python 利用Python编写简单网络爬虫实例3

    利用Python编写简单网络爬虫实例3 by:授客 QQ:1033553122 实验环境 python版本:3.3.5(2.7下报错 实验目的 获取目标网站“http://bbs.51testing. ...

  6. 在weblogic下部署找不到授权文件的解决方法

     很多用户在weblogic上部署的时候,会遇到类似的报错信息,提示授权找不到,解决这个问题的思路如下: 第一步确定授权的没有过期, 客户如果修改了系统时间,会对授权生效产生影响,在进行操作前先将 ...

  7. Oracle存储过程简单实例

    转自 http://www.cnblogs.com/nicholas_f/articles/1526029.html /*不带任何参数存储过程(输出系统日期)*/create or replace p ...

  8. Prometheus Node_exporter 之 CPU Memory Net Disk

    1. CPU type: GraphUnit: shortmax: "100"min: "0"Label: PercentageSystem - cpu 在内核 ...

  9. d3js path generator vs layouts

    我们知道d3的一般套路就是d3.selectAll('path.mypath').data(yourDataset).enter().append('path').attr('class','mypa ...

  10. A B C D类网络地址

    A类网络地址(红色为网络地址,黑色为主机地址): 下限:0000 0001.0000 0000.0000 0000.0000 0000(1.0.0.0) 上限:0111 1110.1111 1111. ...