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 ...
随机推荐
- 后台管理UI+功能
kingRoad功能强大,丰富各种功能和插件,自定义开发功能组件及样式,使用webpack方式集成和纯html两种,UI(自定义开发和AdminLTE的集成优化),弹窗功能使用(Layer弹窗,并与框 ...
- div模拟textarea文本域轻松实现高度自适应——张鑫旭
by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=1362 一.关于tex ...
- 记录在window平台安装python的第三库(py,whl)
在下载python的第三库文件的时候,有些库文件有exe的发行版,但是有些第三库并没有找到针对于window的可执行文件安装包即exe文件,而只有源代码文件即py文件,和whl文件. 下面记录一下在w ...
- shiro标签的使用
guest标签 用户没有身份验证时显示相应信息,即游客访问信息. user标签 用户已经身份验证/记住我登录后显示相应的信息. authenticated标签 用户已经身份验证通过, ...
- Android 后台线程,timertask实现定期更新时间
简述:这是一类定时功能的原型,用来在后台线程中运行一些定时的服务,比如定时修改时间 知识点: 1. Android多线程的消息通信(handler) 2. Java中时间的获取,以及String的格式 ...
- .hivehistory
在当前用户的家目录下有个.hivestory文件,里面存放了用户执行的hive操作记录,如下: [hadoop@hadoop1 hive-0.14]$ cat ~/.hivehistory show ...
- 【项目管理】git和码云的使用
缘起 说了那么多关于git和码云相关的事,一直都没给大伙讲解这个码云究竟是个啥玩意儿. 今天就给大伙说说如何通过git和码云搭建属于自己的代码库. 码云 码云(Git@OSC)是开源中国社区团队推出的 ...
- Linux学习之CentOS(一)----在VMware虚拟机中安装CentOS 7
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...
- Mysql 事务日志(Ib_logfile)
mysql的innodb中事务日志ib_logfile(0/1) 概念:事务日志或称redo日志,在mysql中默认以ib_logfile0,ib_logfile1名称存在,可以手工修改参数,调节开启 ...
- Oracle EBS 应收事务处理取值
SELECT ct.org_id ,ct.attribute1 bu_id --核算主体编号 ,ct.attribute2 dept_id --部门编号 ,hca.account_number ,hp ...