前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN,mcf171专栏。
博客链接:mcf171的博客
——————————————————————————————
Given a 2D board containing 'X'
and 'O'
(the letter 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今天这几个题都很硬啊,很难做动。不过今天这个题提示我了,启发式的用递归需要谨慎,很有可能导致stackoverflow。同样的思路我自己维护一个堆,就没有问题。 Your runtime beats 2.54% of java submissions.
public class Solution {
public void solve(char[][] board) {
if(board.length < 2) return;
if(board[0].length < 2) return;
int[][] visited = new int[board.length][board[0].length];
Map<String,Integer> positions = new HashMap<String,Integer>();
for(int i = 0 ; i < board.length; i ++)
for(int j = 0 ; j < board[0].length; j ++)
if(board[i][j] == 'O')
positions.put(i + "," + j,1);
while(!positions.isEmpty()){
Iterator iterator = positions.keySet().iterator();
String positionStr = (String)iterator.next();
int i = Integer.parseInt(positionStr.split(",")[0]);
int j = Integer.parseInt(positionStr.split(",")[1]);
if(visited[i][j] == 0 && board[i][j] == 'O') {
Stack<int[]> stack = new Stack<int[]>();
List<int[]> data = new ArrayList<int[]>();
stack.push(new int[]{i, j});
boolean flag = true;
while (!stack.empty()) {
int[] position = stack.pop();
positions.remove(position[0] + "," + position[1]);
data.add(position);
int row = position[0], column = position[1];
visited[row][column] = 1;
if (row == 0 || row == board.length - 1) flag = false;
if (column == 0 || column == board[0].length - 1) flag = false;
if (row < board.length - 1 && board[row + 1][column] == 'O' && visited[row + 1][column] == 0)
stack.push(new int[]{row + 1, column});
if (row > 0 && board[row - 1][column] == 'O' && visited[row - 1][column] == 0)
stack.push(new int[]{row - 1, column});
if (column < board[0].length - 1 && board[row][column + 1] == 'O' && visited[row][column + 1] == 0)
stack.push(new int[]{row, column + 1});
if (column > 0 && board[row][column - 1] == 'O' && visited[row][column - 1] == 0)
stack.push(new int[]{row, column - 1});
}
if (flag)
for (int[] position : data) board[position[0]][position[1]] = 'X';
}
}
}
}
感觉还可以提升一下。 Your runtime beats 4.61% of java submissions.
public class Solution {
public void solve(char[][] board) {
if(board.length < 2) return;
if(board[0].length < 2) return;
int[][] visited = new int[board.length][board[0].length];
Map<Integer,Integer> times = new HashMap<Integer,Integer>();
int time = 1;
for(int i = 1 ; i < board.length - 1; i ++)
for(int j = 1 ; j < board[0].length - 1; j ++){
if(visited[i][j] == 0 && board[i][j] == 'O') {
Stack<int[]> stack = new Stack<int[]>();
stack.push(new int[]{i,j});
boolean flag = true;
while(!stack.empty()){
int[] position = stack.pop();
int row = position[0],column = position[1];
visited[row][column] = time;
if(row == 0 || row == board.length - 1)flag = false;
if(column == 0 || column == board[0].length - 1)flag = false;
if(row < board.length - 1 && board[row + 1][column] == 'O' && visited[row + 1][column] == 0) stack.push(new int[]{row + 1,column});
if(row > 0 && board[row - 1][column] == 'O' && visited[row - 1][column] == 0) stack.push(new int[]{row - 1,column});
if(column < board[0].length - 1 && board[row][column + 1] == 'O' && visited[row][column + 1] == 0) stack.push(new int[]{row,column + 1});
if(column > 0 && board[row][column - 1] == 'O' && visited[row][column - 1] == 0) stack.push(new int[]{row,column - 1});
}
if(flag)
times.put(time, 1);
time ++;
}
if(times.containsKey(visited[i][j])) board[i][j] = 'X';
}
}
}