五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

力扣:劍指 Offer 29. 順時(shí)針打印矩陣

2023-03-14 19:05 作者:薄荷硬糖醬  | 我要投稿

題目:

劍指 Offer 29. 順時(shí)針打印矩陣

難度簡(jiǎn)單509收藏分享切換為英文接收動(dòng)態(tài)反饋

輸入一個(gè)矩陣,按照從外向里以順時(shí)針的順序依次打印出每一個(gè)數(shù)字。

?

示例 1:

輸入:matrix = [[1,2,3],[4,5,6],[7,8,9]]輸出:[1,2,3,6,9,8,7,4,5]

示例 2:

輸入:matrix =?[[1,2,3,4],[5,6,7,8],[9,10,11,12]]輸出:[1,2,3,4,8,12,11,10,9,5,6,7]

?

限制:

  • 0 <= matrix.length <= 100

  • 0 <= matrix[i].length?<= 100

注意:本題與主站 54 題相同:https://leetcode-cn.com/problems/spiral-matrix/

第一種對(duì)法:

class?Solution?{

public:

????vector<int>?spiralOrder(vector<vector<int>>&?matrix)?{

????????if(matrix.size()==0||matrix[0].size()==0){

????????????return?{};

????????}

????????vector<int>?s;

????????int?col=matrix[0].size();

????????int?row=matrix.size();

????????int?right,left,top,bottom;

????????right=col-1,left=0;

????????top=0,bottom=row-1;

????????int?cnt=0;

????????while(right>=left&&top<=bottom){

????????????for(int?i=left;i<=right;i++){

????????????????s.push_back(matrix[top][i]);

????????????????cnt++;

????????????????if(cnt==col*row){

????????????????????return?s;

????????????????}

????????????}

????????????for(int?i=top+1;i<=bottom;i++){

????????????????s.push_back(matrix[i][right]);

????????????????cnt++;

????????????????if(cnt==col*row){

????????????????????return?s;

????????????????}

????????????}

????????????for(int?i=right-1;i>=left;i--){

????????????????s.push_back(matrix[bottom][i]);

????????????????cnt++;

????????????????????if(cnt==col*row){

????????????????????????return?s;

????????????????????}

????????????}

????????????for(int?i=bottom-1;i>top;i--){

????????????????s.push_back(matrix[i][left]);

????????????????cnt++;

????????????????????if(cnt==col*row){

????????????????????????return?s;

????????????????????}

????????????}

????????????left++,right--;

????????????top++,bottom--;

????????}

????????return?s;

????}

};

注意細(xì)節(jié):不要把橫或列搞錯(cuò),不要把循環(huán)寫錯(cuò)

力扣:劍指 Offer 29. 順時(shí)針打印矩陣的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
菏泽市| 绩溪县| 金阳县| 临猗县| 当阳市| 大石桥市| 高雄市| 高邮市| 阜城县| 波密县| 秦皇岛市| 昌图县| 广东省| 遵义市| 莒南县| 自贡市| 榆中县| 溧水县| 育儿| 云霄县| 孟津县| 城固县| 青冈县| 鸡泽县| 积石山| 保定市| 扶风县| 思茅市| 威远县| 临泉县| 营山县| 潼南县| 图们市| 临沂市| 满城县| 松潘县| 重庆市| 通许县| 邮箱| 岚皋县| 公安县|