【LeetCode】566 Reshape the Matrix
来源:互联网 发布:系统封装软件 编辑:程序博客网 时间:2024/06/06 20:16
原题地址:566. Reshape the Matrix。
解法一
vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) { int len1 = nums.size(); int len2 = nums[0].size(); vector<vector<int>> res; vector<int> line; if (len1 * len2 == r * c) { for (int i = 0; i < len1; i++) { for (int j = 0; j < len2; j++) { line.push_back(nums[i][j]); } } for (int i = 0; i < r; i++) { vector<int> temp; for (int j = 0; j < c; j++) { temp.push_back(line[i * c + j]); } res.push_back(temp); } } else { res = nums; } return res;}
解法二
vector<vector<int>> matrixReshape2(vector<vector<int>>& nums, int r, int c) { int len1 = nums.size(); int len2 = nums[0].size(); vector<vector<int>> res(r, vector<int>(c, 0)); if (len1 * len2 == r * c) { for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { int k = i * c + j; res[i][j] = nums[k / len2][k % len2]; } } } else { res = nums; } return res;}
参考:
[C++] Clean Code - 5 lines (2 Solution)
最后更新于2017年6月19日。
阅读全文
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
- Reshape the Matrix(leetcode)
- leetCode-Reshape the Matrix
- Array-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之Reshape the Matrix
- LeetCode:566. Reshape the Matrix
- leetcode 566. Reshape the Matrix
- LeetCode | 566. Reshape the Matrix
- leetcode-566. Reshape the Matrix
- ReactNative 的开发工具WebStorm破解版使用+React Native 终端常用命令
- 离线文档-Animation
- 使用qrcode.js生成二维码图片
- opencv序列图像ROI获取
- iOS从info.plist获取项目的名称及版本号
- 【LeetCode】566 Reshape the Matrix
- MFC创建文件并写入文件数据
- Unity游戏UI框架(五):配置管理
- div中的内容垂直居中的五种方法
- PHP文件上传实现上传到另一台服务器
- Java 反射 java.lang.reflect包
- 图文详解YUV420数据格式
- Mac平台的docker 代理之两个privoxy
- 技术博客二:Handler的机制