Leetcode: Surrounded Regions

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   ar   for   sp   div   

Given a 2D board containing ‘X‘ and ‘O‘, capture all regions surrounded by ‘X‘.A region is captured by flipping all ‘O‘s into ‘X‘s in that surrounded region.For example,X X X XX O O XX X O XX O X XAfter running your function, the board should be:X X X XX X X XX X X XX O X X

難度:92。這個題目用到的方法是圖形學中的一個常用方法:Flood fill演算法,其實就是從一個點出發對周圍地區進行目標顏色的填充。背後的思想就是把一個矩陣看成一個圖的結構,每個點看成結點,而邊則是他上下左右的相鄰點,然後進行一次廣度或者深度優先搜尋。

這道題首先四個邊緣上的‘O’點都不是被surrounded的,這是很直接能看出的,麻煩的是與這些邊界上的‘O’點毗鄰的其他‘O’點,這些點由於跟邊緣上的‘O‘毗鄰,所以也米有被‘X’包裹住。所以我們的想法是:把邊界上的‘O’點都找出來,對它們做Flood Fill, 把聯通的‘O’地區找出來,把這個地區的點統統由‘O’替換為其他字元比如‘$’。這樣沒有被替換仍舊為‘O’的那些點,就是被‘X’包裹的。這樣整體掃描一次,剩下的所有‘O‘都應該被替換成‘X‘,而‘$‘那些最終應該是還原成‘O‘。

複雜度分析上,我們先對邊緣做Flood fill演算法,因為只有是‘O‘才會進行,而且會被替換成‘#‘,所以每個結點改變次數不會超過一次,因而是O(m*n)的複雜度,最後一次遍曆同樣是O(m*n),所以總的時間複雜度是O(m*n)。

空間上沒懂,看了別人的思路。空間上就是遞迴棧(深度優先搜尋)或者是隊列(廣度優先搜尋)的空間,同時存在的空間佔用不會超過O(m+n)(以廣度優先搜尋為例,每次隊列中的結點雖然會往四個方向拓展,但是事實上這些結點會有很多重複,假設從中點出發,可以想象最大的擴充不會超過一個菱形,也就是n/2*2+m/2*2=m+n,所以演算法的空間複雜度是O(m+n))

 1 public class Solution { 2     public void solve(char[][] board) { 3         if (board==null || board.length==0 || board[0].length==0) return; 4         for (int i=0; i<board[0].length; i++) { 5             if (board[0][i] == ‘O‘) { 6                 board[0][i] = ‘$‘; 7                 floodFill(board, 0*board[0].length+i); 8             } 9             if (board[board.length-1][i] == ‘O‘) {10                 board[board.length-1][i] = ‘$‘;11                 floodFill(board, (board.length-1)*board[0].length+i);12             }13         }14         for (int i=0; i<board.length; i++) {15             if (board[i][0] == ‘O‘) {16                 board[i][0] = ‘$‘;17                 floodFill(board, i*board[0].length+0);18             }19             if (board[i][board[0].length-1] == ‘O‘) {20                 board[i][board[0].length-1] = ‘$‘;21                 floodFill(board, i*board[0].length+board[0].length-1);22             }23         }24         25         for (int i=0; i<board.length; i++) {26             for (int j=0; j<board[0].length; j++) {27                 if (board[i][j] == ‘X‘) continue;28                 if (board[i][j] == ‘$‘) {29                     board[i][j] = ‘O‘;30                 }31                 else if (board[i][j] == ‘O‘) {32                     board[i][j] = ‘X‘;33                 }34             }35         }36     }37     38     public void floodFill(char[][] board, int num) {39         LinkedList<Integer> queue = new LinkedList<Integer>();40         queue.add(num);41         while (queue.size() !=0) {42             int temp = queue.poll();43             int row = temp/board[0].length;44             int col = temp%board[0].length;45             if (row<board.length-1 && board[row+1][col]==‘O‘) {46                 board[row+1][col] = ‘$‘;47                 queue.add((row+1)*board[0].length + col);48             }49             if (row>0 && board[row-1][col]==‘O‘) {50                 board[row-1][col] = ‘$‘;51                 queue.add((row-1)*board[0].length + col);52             }53             if (col<board[0].length-1 && board[row][col+1]==‘O‘) {54                 board[row][col+1] = ‘$‘;55                 queue.add(row*board[0].length + col + 1);56             }57             if (col>0 && board[row][col-1]==‘O‘) {58                 board[row][col-1] = ‘$‘;59                 queue.add(row*board[0].length + col - 1);60             }61         }62     }63 }

 

Leetcode: Surrounded Regions

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.