欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

leetCode题解之Reshape the Matrix

发布时间:2025/3/15 33 豆豆
生活随笔 收集整理的这篇文章主要介绍了 leetCode题解之Reshape the Matrix 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1、题目描述

2、分析

使用了一个队列。

 

3、代码

1 vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) { 2 3 if( nums.size() * nums[0].size() != r * c ) 4 return nums; 5 vector<vector<int>> ans; 6 queue<int> q; 7 for( size_t i = 0; i < nums.size() ;i++ ) 8 for( size_t j =0; j < nums[0].size() ;j++) 9 q.push( nums[i][j] ); 10 11 vector<int> col; 12 while( !q.empty() ) 13 { 14 col.push_back( q.front() ); 15 q.pop(); 16 if( col.size() == c ) 17 { 18 ans.push_back( col ); 19 col.clear(); 20 } 21 } 22 23 return ans; 24 25 }

 

转载于:https://www.cnblogs.com/wangxiaoyong/p/8929536.html

总结

以上是生活随笔为你收集整理的leetCode题解之Reshape the Matrix的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。