国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當前位置: 首頁 > news >正文

目前做批發(fā)比較好的b2b網(wǎng)站百度免費發(fā)布信息

目前做批發(fā)比較好的b2b網(wǎng)站,百度免費發(fā)布信息,房地產(chǎn)銷售真實工資,平羅縣住房和城鄉(xiāng)建設局網(wǎng)站LeetCode:48. 旋轉(zhuǎn)圖像 受到力扣hot100:54. 螺旋矩陣的啟發(fā),我們可以對旋轉(zhuǎn)圖像按層旋轉(zhuǎn),我們只需要記錄四個頂點,并且本題是一個方陣,四個頂點就能完成圖像的旋轉(zhuǎn)操作。 1、逐層旋轉(zhuǎn) 注意到&#xff0…

LeetCode:48. 旋轉(zhuǎn)圖像
在這里插入圖片描述

受到力扣hot100:54. 螺旋矩陣的啟發(fā),我們可以對旋轉(zhuǎn)圖像按層旋轉(zhuǎn),我們只需要記錄四個頂點,并且本題是一個方陣,四個頂點就能完成圖像的旋轉(zhuǎn)操作。

1、逐層旋轉(zhuǎn)

注意到,一層的四個頂點存在一定的位置關(guān)系,我們只需要記錄四個值:
top_row、bottom_row、left_col、right_col,則上右下左四個頂點分別為:

  • (top_row,left_col)、(top_row,right_col)、(bottom_row,right_col)、(bottom_row,left_col)

當我們需要更新層時,注意矩陣的下標,只需進行如下操作:

  • top_row++
  • bottom_row--
  • left_col++
  • right_col--

這樣我們就找到了一層的四個頂點,以及更新層的操作。

現(xiàn)在我們只需要逐層更新即可。
時間復雜度: O ( n 2 ) O(n^2) O(n2)
空間復雜度: O ( 1 ) O(1) O(1)

在這里插入圖片描述

class Solution {
public:void rotate(vector<vector<int>>& matrix) {int top_row = 0, left_col = 0;int bottom_row = matrix.size() - 1, right_col = matrix.size() - 1;//由于size() > 1,所以可以這樣做while(top_row < bottom_row){//方陣,結(jié)束條件int step = right_col - left_col;for(int i = 0; i < step; ++ i){int temp;//上換到右temp = matrix[top_row + i][right_col];matrix[top_row + i][right_col] = matrix[top_row][left_col + i];//右換到下int temp2 = temp;temp = matrix[bottom_row][right_col - i];matrix[bottom_row][right_col - i] = temp2;//下?lián)Q到左temp2 = temp;temp = matrix[bottom_row - i][left_col];matrix[bottom_row - i][left_col] = temp2;//左換到上matrix[top_row][left_col + i] = temp;}//更新層top_row++;bottom_row--;left_col++;right_col--;}return ;}
};
  • 我們需要注意一個問題,判斷結(jié)束條件時,由于方陣行數(shù)是n可以是偶數(shù)也可以是奇數(shù),奇數(shù)時,上行和下行相等則結(jié)束。但如果是偶數(shù)時,他倆會交叉,因此是下行大于上行時結(jié)束!
    • 為了在編程時忽略奇偶數(shù)的這個問題,我們可以編程時將判斷條件更寬泛
    • 如果top_row > bottom_row也不滿足條件,那不要寫top_row == bottom_row,而是將兩者結(jié)合起來寫,這樣可以避免自己的遺漏。

為了節(jié)省臨時變量,我們也可以按左下轉(zhuǎn)到左上,右下轉(zhuǎn)到左下,右上轉(zhuǎn)到右下,左上轉(zhuǎn)到右上的順序旋轉(zhuǎn),這樣只需要存儲左上的值即可。

class Solution {
public:void rotate(vector<vector<int>>& matrix) {int top_row = 0, left_col = 0;int bottom_row = matrix.size() - 1, right_col = matrix.size() - 1;//由于size() > 1,所以可以這樣做while(top_row < bottom_row){//方陣,結(jié)束條件int step = right_col - left_col;for(int i = 0; i < step; ++ i){int temp = matrix[top_row][left_col + i];matrix[top_row][left_col + i] = matrix[bottom_row - i][left_col];左換到上matrix[bottom_row - i][left_col] = matrix[bottom_row][right_col - i];//下?lián)Q到左matrix[bottom_row][right_col - i] = matrix[top_row + i][right_col];//右換到下matrix[top_row + i][right_col] = temp;//上換到右}//更新層top_row++;bottom_row--;left_col++;right_col--;}return ;}
};

和官解的方法二類似。

2、兩次翻轉(zhuǎn)等于旋轉(zhuǎn)

在這里插入圖片描述

class Solution {
public:void rotate(vector<vector<int>>& matrix) {int n = matrix.size();// 水平翻轉(zhuǎn)for (int i = 0; i < n / 2; ++i) {for (int j = 0; j < n; ++j) {swap(matrix[i][j], matrix[n - i - 1][j]);}}// 主對角線翻轉(zhuǎn)for (int i = 0; i < n; ++i) {for (int j = 0; j < i; ++j) {swap(matrix[i][j], matrix[j][i]);}}}
};
http://m.aloenet.com.cn/news/42816.html

相關(guān)文章:

  • 創(chuàng)建視頻網(wǎng)站昆明百度搜索排名優(yōu)化
  • 織夢高端html5網(wǎng)站建設工作室網(wǎng)絡公司網(wǎng)站模板高端營銷型網(wǎng)站建設
  • 信陽做網(wǎng)站 漢獅網(wǎng)絡短視頻seo詢盤系統(tǒng)
  • 秦皇島做網(wǎng)站公司win10優(yōu)化大師怎么樣
  • 備案期間怎么做網(wǎng)站磁力
  • 石家莊網(wǎng)站建設策劃方案杭州seo搜索引擎優(yōu)化
  • 域名停域app免費下載上海還能推seo嗎
  • 商城類的網(wǎng)站怎么做百度百度一下首頁
  • 做服裝設計兼職的網(wǎng)站短信廣告投放
  • 房地產(chǎn)型網(wǎng)站建設社區(qū)推廣方法有哪些
  • 做旅游網(wǎng)站官方百度下載安裝
  • 備案用網(wǎng)站建設方案書又一病毒來了比新冠可怕
  • 金華做網(wǎng)站報價seo關(guān)鍵字優(yōu)化軟件
  • 免費網(wǎng)站建設作業(yè)總結(jié)seo的作用主要有
  • 做電子商務系統(tǒng)網(wǎng)站建設seo的搜索排名影響因素有哪些
  • 手機網(wǎng)站Com臺州做優(yōu)化
  • .net 網(wǎng)站開發(fā)視頻教程系統(tǒng)優(yōu)化軟件有哪些
  • 做外貿(mào)找產(chǎn)品上哪個網(wǎng)站好引流用什么話術(shù)更吸引人
  • t恤定制網(wǎng)站哪個好濟南seo網(wǎng)站優(yōu)化公司
  • 做藥公司的網(wǎng)站前置審批程序員培訓機構(gòu)排名
  • 杭州做網(wǎng)站hzfwwl寧波seo網(wǎng)絡推廣代理公司
  • 用網(wǎng)站做淘寶客的人多嗎有哪些搜索引擎網(wǎng)站
  • 花園休閑平臺設計百度seo關(guān)鍵詞排名查詢工具
  • 建網(wǎng)站做站在網(wǎng)絡營銷的現(xiàn)狀和發(fā)展趨勢
  • 茂名網(wǎng)站建設托管專業(yè)競價托管哪家好
  • 網(wǎng)站建設總結(jié)報告營銷策劃公司經(jīng)營范圍
  • 太原做網(wǎng)站聯(lián)系方式近三天新聞50字左右
  • 青島建設監(jiān)理協(xié)會網(wǎng)站百度seo優(yōu)化教程免費
  • 網(wǎng)站開發(fā) 項目的招標文件百度廣告平臺電話
  • 網(wǎng)站建設福州產(chǎn)品網(wǎng)絡推廣的方法