classSolution{int m =0;int n =0;publicvoidsolve(char[][] board){
m = board.length;if(m ==0)return;
n = board[0].length;for(int i =0; i < n; i++){if(board[0][i]=='O')dfs(board,0, i);if(board[m -1][i]=='O')dfs(board, m -1, i);}for(int i =1; i < m -1; i++){if(board[i][0]=='O')dfs(board, i,0);if(board[i][n -1]=='O')dfs(board, i, n -1);}for(int i =0; i < m; i++){for(int j =0; j < n; j++){if(board[i][j]=='X')continue;elseif(board[i][j]=='O') board[i][j]='X';else board[i][j]='O';}}}publicvoiddfs(char[][] board,int x,int y){if(x >= m || x <0|| y >= n || y <0|| board[x][y]!='O')return;
board[x][y]='M';dfs(board, x -1, y);dfs(board, x, y +1);dfs(board, x +1, y);dfs(board, x, y -1);}}
131.分割回文串(回溯)
classSolution{public List<List<String>>partition(String s){
List<List<String>> res =newArrayList<>();if(s.length()<1)return res;
Deque<String> path =newArrayDeque<>();dfs(s,0, path, res);return res;}publicvoiddfs(String s,int start, Deque<String> path, List<List<String>> res){if(start == s.length()){
res.add(newArrayList<>(path));return;}for(int i = start; i < s.length(); i++){if(!check(s, start, i))continue;
path.addLast(s.substring(start, i +1));dfs(s, i +1, path, res);
path.removeLast();}}publicbooleancheck(String s,int start,int end){while(start <= end){if(s.charAt(start)!= s.charAt(end))returnfalse;
start++;
end--;}returntrue;}}