566. Reshape the Matrix
来源:互联网 发布:怎么样数据共享 编辑:程序博客网 时间:2024/05/29 19:02
将一个数组变成r*c的数组,获得数组长度,如果和r*s不同,就返回原矩阵,否则,就每行读取,给新矩阵每行进行赋值。
java代码:(8ms,不是很快诶==)
public int[][] matrixReshape(int[][] nums, int r, int c) {int sum = nums.length*nums[0].length;
if(r*c==sum) {
int new_sum = 0;
int[][] new_nums = new int[r][c];
for(int i=0;i<nums.length;i++) {
for(int j=0;j<nums[i].length;j++) {
new_sum++;
int m = new_sum/c;
int n = new_sum%c;
if(n==0) {
new_nums[m-1][c-1] = nums[i][j];
}
else {
new_nums[m][n-1] = nums[i][j];
}
}
}
return new_nums;
}else {
return nums;
}
}
阅读全文
0 0
- 566. Reshape the Matrix
- 566.Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- 566. Reshape the Matrix
- ssh远程执行 测试
- 我的词条 之 偏移地址
- JAVA Debug 调试 java application JUnit单元测试
- vm中的disk负载观察iostat的%util不科学
- 递归 (斐波那契数的简单两种应用)
- 566. Reshape the Matrix
- 面试中经常问到javascript的this,你知道多少?
- 欢迎使用CSDN-markdown编辑器
- Android遇到Error:Execution failed for task ':app:mergeDebugResources'. > Error: java.ut和java.lang.Clas
- jQuery生成二维码
- JAVA知识_12
- 属性动画
- android常用控件
- (一)JMS初识