Flash Speed Questions

The solution time is much shorter than you think.

/* 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 x x o o x x x o x x o x x after running your function, the board should be: x x x x x x x x x x x x x o x x */ public class solution { // this algorithm cannot solve the large test set public void solve(char[][] board) { // start typing your java solution below // do not write main() function int rows = board. length; if(rows == 0) return; int cols = board[0].length; for(int i = 0; i < cols; i++) { // check first row's o if(board[0][i] == 'o') { // change it to other symbol board[0][i] = '#'; dfs(board, 0, i); } // check the last row if(board[rows - 1][i] == 'o') { board[rows - 1][i] = '#'; dfs(board, rows - 1, i); } } for(int i = 0; i < rows; i++) { // check first col if(board[i][0] == 'o') { board[i][0] = '#'; dfs(board, i, 0); } // check last col if(board[i][cols - 1] == 'o') { board[i][cols - 1] = '#'; dfs(board, i, cols - 1); } } // change o to x changeto(board, 'o', 'x'); // change # to o changeto(board, '#', 'o'); return; } public void dfs(char[][] board, int row, int col) { // check up if(row > 0) { if(board[row - 1][col] == 'o') { board[row - 1][col] = '#'; dfs(board, row - 1, col); } } // check left if(col > 0) { if(board[row][col - 1] == 'o') { board[row][col - 1] = '#'; dfs(board, row, col - 1); } } // check right if(row < board. length - 1) { if(board[row + 1][col] == 'o') { board[row+1][col] = '#'; dfs(board, row+1, col); } } // check down if(col < board[0].length - 1) { if(board[row][col+1] == 'o'){ board[row][col+1] = '#'; dfs(board, row, col+1); } } return; } public void changeto(char[][] board, char from, char to) { for(int i = 0; i < board. length; i++) { for(int j = 0; j < board[0].length; j++) { if(board[i][j] == from) { board[i][j] = to; } } } return; } } Get the answer
Category: chemistry | Author: Valko Tomer

art

Selma Yafa 55 Minutes ago

/* write a method called fractionsum that accepts an integer parameter n and * returns as a double the sum of the first n terms of the sequence: * s

chemistry

Sagi Boris 1 Hours ago

/* write an application that prompts the user for the radius of a circle and uses a method called circlearea to calculate the area of the circle */imp

business

Sagi Boris 1 Hours ago

/** * ramanujan. java * * s. ramanujan was an indian mathematician who became famous for his intuition * for numbers. when the english mathematician