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. [合集]解决Python报错:local variable 'xxx' referenced before assignment

    a = 1 def use(): print(a) #输出1 引用不会报错 a = 1 def use(): a = 3 print(a) #输出 3 重新赋值也不会报错. 局部变量会优先在函数内部去 ...

  2. 【学习笔记】--- 老男孩学Python,day18 面向对象------继承

    继承 继承是一种创建新类的方式,在python中,新建的类可以继承一个或多个父类, 父类又可称为基类或超类,新建的类称为派生类或子类 python中类的继承分为:单继承和多继承 class Fathe ...

  3. python学习之老男孩python全栈第九期_数据库day001知识点总结 —— MySQL操作数据库以及数据表、基本数据类型、基本增删改查、外键定义以及创建

    一. 学习SQL语句规则以及外键 1. 操作文件夹 create database db2; 创建文件夹 create database db2 default charset utf8; 创建文件夹 ...

  4. spring AOP 动态代理和静态代理以及事务

    AOP(Aspect Oriented Programming),即面向切面编程 AOP技术,它利用一种称为"横切"的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装 ...

  5. Unity3D 场景切换加载进度条实现

    需要三个场景,场景A,场景B,场景C: 场景A:一个按钮,点击加载场景B: 场景B:从A切换到C过度场景,加载进度条: 场景C:目标场景: 创建OnProgress.cs脚本: using Syste ...

  6. thymeleaf 标签使用方法

    使用thymeleaf首先添加依赖,<dependency><groupId>org.springframework.boot</groupId><artif ...

  7. http常见状态码有哪些?

    ajax常见面试题 1:什么是ajax?ajax作用是什么? 异步的javascript和xml AJAX 是一种用于创建快速动态网页的技术. ajax用来与后台交互 2:原生js ajax请求有几个 ...

  8. 关于HSTS的总结

    访问http网站,和服务器交互的步骤浏览器向服务器发起一次HTTP请求服务器返回一个重定向地址浏览器在发送一次HTTPS请求,得到最终内容 上面浏览器发送http请求后容易被拦截,使用HSTS后可以避 ...

  9. DOM增删改操作

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  10. 你真的了解View的坐标吗?

    闲聊 View,对我们来说在熟悉不过了,从接触 Android 开始,我们就一直在接触 View,界面当中到处都是 View,比如我们经常用到的 TextView,Button,LinearLayou ...