leetcode566. Reshape the Matrix
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的更多相关文章
- Leetcode566.Reshape the Matrix重塑矩阵
在MATLAB中,有一个非常有用的函数 reshape,它可以将一个矩阵重塑为另一个大小不同的新矩阵,但保留其原始数据. 给出一个由二维数组表示的矩阵,以及两个正整数r和c,分别表示想要的重构的矩阵的 ...
- LeetCode 566. 重塑矩阵(Reshape the Matrix)
566. 重塑矩阵 566. Reshape the Matrix 题目描述 LeetCode LeetCode LeetCode566. Reshape the Matrix简单 Java 实现 c ...
- [Swift]LeetCode566. 重塑矩阵 | Reshape the Matrix
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...
- Reshape the Matrix
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...
- LeetCode 566. Reshape the Matrix (重塑矩阵)
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...
- leetcode算法:Reshape the Matrix
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...
- [LeetCode] Reshape the Matrix 重塑矩阵
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...
- 566. Reshape the Matrix
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...
- LeetCode 566 Reshape the Matrix 解题报告
题目要求 In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ...
随机推荐
- PHPCMS V9标签循环嵌套调用数据的方法
PHPCMS V9的标签制作以灵活见长,可以自由DIY出个性的数据调用,对于制作有风格有创意的网站模板很好用,今天就介绍一个标签循环嵌套方法,可以实现对PC标签循环调用,代码如下: 在此文件里/php ...
- js-权威指南学习笔记15.2
1.读取Element的innerHTML属性作为字符串标记返回那个元素的内容. 2.当设置元素的outerHTML时,元素本身被新的内容所替换.只有Element节点定义了outerHTML属性,D ...
- python 字符串的方法和注释
capitalize() 把字符串的第一个字符改为大写 casefold() 把整个字符串的所有字符改为小写 center(width) 将字符串居中,并使用空格填充至长度 width 的新字符串 c ...
- 解决MyEclipse不能导出war包
原因:无法导出是由于软件破解不完成导致的: 解决办法: 找到MyEclipse安装目录下MyEclipse\Common\plugins文件夹中的com.genuitec.eclipse.export ...
- 【转】stropts.h: No such file or directory – How to Fix
原文地址:stropts.h: No such file or directory – How to Fix 作者:xjc2694 It is a known issue that modern Li ...
- eclipse工具的安装配置
安装环境 系统:Windows7 软件:jre-8u73-windows-x64.exe,eclipse-inst-win64.exe Eclipse的安装过程 1.安装jre-8u73-window ...
- C# winform 只运行一个应用程序
应用程序只有一个实例,当启动一次时创建实例,当多次启用时激活当前实例. 创建一个单利管理类 using Microsoft.VisualBasic.ApplicationServices; publi ...
- 关于Entity Framework更新的几种方式以及可能遇到的问题(附加类型“Model”的实体失败,因为相同类型的其他实体已具有相同的主键值)在使用 "Attach" 方法或者将实体的状态设置为 "Unchanged" 或 "Modified" 时如果图形中的任何实体具有冲突键值,则可能会发生上述行为
在日常使用Entity Framework中,数据更新通常会用到.下面就简单封装了一个DBContext类 public partial class EFContext<T> : DbCo ...
- npm install时报错“Unexpected end of JSON input while parsing near...”解决方法
执行:npm cache clean --force 即可解决此问题
- jquery如何设置与去除disabled属性?五种方法
//两种方法设置disabled属性 $('#areaSelect').attr("disabled",true); $('#areaSelect').attr("dis ...