LeetCode:566. Reshape the Matrix
来源:互联网 发布:免费网络图书馆 编辑:程序博客网 时间:2024/06/06 08:45
代码:
class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
int m = nums.size(), n = nums[0].size();
if (m * n != r * c) {
return nums;
}
vector<vector<int>> res(r, vector<int>(c, 0));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int k = i * n + j;
res[k / c][k % c] = nums[i][j];
}
}
return res;
}
};
思路:
(1)获得原矩阵的元素个数,与新矩阵的元素个数比较,如果两者元素个数不一致,那么返回原矩阵
(2)如果元素个数一致,那么将原矩阵里的元素按行排列提取出来,然后按行排列输入到新矩阵里面
0 0
- leetcode 566. Reshape the Matrix
- LeetCode 566. Reshape the Matrix
- [LeetCode]566. Reshape the Matrix
- [leetcode]566. Reshape the Matrix
- LeetCode:566. Reshape the Matrix
- leetcode 566. Reshape the Matrix
- LeetCode | 566. Reshape the Matrix
- leetcode-566. Reshape the Matrix
- LeetCode 566.Reshape the Matrix
- LeetCode: 566. Reshape the Matrix
- LeetCode-566. Reshape the Matrix
- LeetCode * 566.Reshape the Matrix
- LeetCode: 566. Reshape the Matrix
- leetcode.566.Reshape the Matrix
- LeetCode 566. Reshape the Matrix
- [LeetCode] 566. Reshape the Matrix
- 【LeetCode】566. Reshape the Matrix
- [LeetCode]566. Reshape the Matrix
- Android 6.0 运行时权限处理完全解析 及 三方库的使用
- CSS 变量教程
- Java中实现String.padLeft和String.padRight
- 在Domino平台上应用Java 反射机制
- SSO单点登录原理
- LeetCode:566. Reshape the Matrix
- Tomcat+MySQL为自己的APP打造服务器(3-1)Android 和 Service 的交互之GET方式
- JAVA 内存划分
- docker 循环依赖的网络设置
- spark入门介绍(菜鸟必看)
- 软件性能测试_loadrunner之web_custom_request应用示例
- iOS 四舍五入保留两位小数
- python中使用from..import时,提示SyntaxError:invalid syntax
- 海康、大华设备搜索大概实现原理