首页 > 编程知识 正文

一维数组遍历二维数组,对一个不规则的二维数组进行遍历

时间:2023-05-04 01:41:40 阅读:180562 作者:963

59螺旋函数2

这和之前的螺旋函数一样,只是直接知道数组,让它遍历。 现在可以遍历输入数组,可以使用完全相同的解法

类解决方案{公共: vectorvectorintgeneratematrix (intn ) vectorvectorintRES; for(intI=0; i n; I () RES[I].resize ) ) n; (} int left=0; int right=n - 1; int top=0 int bottom=n - 1; int count=1; wile(left=righttop=bottom ) for ) intI=left; i=right; I ) { res[top][i]=count; }for(intI=top1; I=Bi ) { res[i][right]=count; (if ) leftrighttopbottom ) for ) intI=right-1; i=left; I--}{RES[Bottom][I]=count; }for(intI=Bottom-1; i top; I--}{RES[I][left]=count; } } left; right--; top; bottom--; } return res; };

时间复杂度o(n*n ),空间复杂度o ) )1) ) ) )。

498对角线遍历

这也是二维排列扫描问题,感觉没有凹坑和应该注意的算法。 抓住规律扫描他,但是! 呃! 呃!

为什么一直在报告错误? 是什么? 是什么? 自己怎么计算都是对的,为什么一直报错? 是什么? 是什么? 是什么?

class solution { public : vectorintfinddiagonalorder (vectorvectorintmat ) { int m=mat.size ); int n=mat[0].size (; int row=0; int col=0; vectorint res; res.push_back(mat[row][col]; wile(row!=m || col!=n () /右上while(coln ) if ) row--=0) RES.push_back ) mat[row][col] ); //右上else{ //右row=0; //如果超出边界,则返回边界RES.push_back(mat[row][col] ); 黑; (//左下while(rowm ) if ) col--=0) RES.push_back ) mat[row][col] ); else{ col=0; //如果超出边界,则返回边界RES.push_back(mat[row][col] ); 黑; } } } return res; }; 一直在line 1034: char 34: runtime error : additionofunsignedoffsetto0x 603000000010 overflowed to0x 602 ffffff8(STL _ vector sumer sumed it izer : undefined-behavior/usr/SD dwd/./lib/gcc/x86 _ 64-Linux-GNU/9/9

如果调查的话,恐怕是数组越界了,或者出现了不确定的下标,为什么最简单的例子也会这样呢? 是什么? 是什么?

换了两个小时还不知道问题的我崩溃了

明天试着写完全函数gdb吧。 很难。

果然修补臭虫不能拼命。 昨天两个小时没能出来,今天两分钟就解决。 真傻,又犯了一个简单的问题,把col n变成col n; 请先比较,把正数写在前面,认为比较起来前后没有区别

,变更后,如果遇到[ [ 1,2 ] ]的例子就会超时。 因为无法判断周期的结束条件

中选择另一种天花板类型

class solution { public : vectorintfinddiagonalorder (vectorvectorintmat ) { int m=mat.size ); int n=mat[0].size (; int row=0; int col=0; vectorint res; res.push_back(mat[row][col]; while(row=mcol=n ) (/右上while ) coln ) if(-row=0) RES.push_back ) mat[row][col] ); //右上else{ //右row=0; //如果超出边界,则返回边界RES.push_back(mat[row][col] ); 黑; }//左下while(rowm ) if(-col=0) RES.push_back ) mat[row][col] ); else{ col=0; //如果超出边界,则返回边界RES.push_back(mat[row][col] ); 黑; } } } return res; }; 已经结束了

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。