孤军奋战并不快乐
上周竞赛也参加了,但是就写出来了第一道题,再加上周一点私事,就没有写题解…
题目链接
总的来说,前两道是阅读理解,没有难度,第三道是BFS,第四道是DFS+暴力
这是个纯粹的阅读理解题+签到题,按照我们正常的逻辑走就完全可以拿到结果
对于例子:n = 2, m = 3, indices = [[0,1],[1,1]]
int[n][m]
的二维数组indices
的第一个值[0,1]
,之后我们对二维数组的第0行和第1列分别++indices
的下一个值[1,1]
,之后我们对二维数组的第1行和第1列分别++ public int oddCells(int n, int m, int[][] indices) {
int[][] arr = new int[n][m];
int count = 0;
for (int i = 0; i < indices.length; i ++) {
int row = indices[i][0];
int column = indices[i][1];
for (int r = 0; r < n; r ++) arr[r][column] ++;
for (int c = 0; c < m; c ++) arr[row][c] ++;
}
for (int i = 0; i < n; i ++)
for (int j = 0; j < m; j ++) if (arr[i][j] % 2 != 0) count ++;
return count;
}
这个也暴力就完事了
对于例子:upper = 2, lower = 1, colsum = [1,1,1]
int[2][colsum.length]
colsum[i] == 2
那么upper和lower都–colsum[i] == 1
那么upper --,如果upper == 0, 就lower –PS 这个题我不太会用List
所以先用数组,然后转成了ArrayList
public List<List<Integer>> reconstructMatrix(int upper, int lower, int[] colsum) {
List<List<Integer>> row = new ArrayList<>(2);
int length = colsum.length;
int[][] arr = new int[2][length];
for (int i = 0; i < length; i ++) {
if (colsum[i] == 2) {
if (upper <= 0) return row;
arr[0][i] = 1;
upper --;
}
}
for (int i = 0; i < length; i ++) {
if (upper == 0) break;
if (colsum[i] == 1) {
arr[0][i] = 1;
upper --;
}
}
for (int i = 0; i < length; i ++) {
if (colsum[i] == 2 || (colsum[i] == 1 && arr[0][i] != 1)) {
if (lower <= 0) return row;
arr[1][i] = 1;
lower --;
}
}
if (lower > 0 || upper > 0) return row;
List<Integer> column1 = new ArrayList<>(length);
List<Integer> column2 = new ArrayList<>(length);
for (int i = 0; i < length; i ++) {
column1.add(arr[0][i]);
column2.add(arr[1][i]);
}
row.add(column1);
row.add(column2);
return row;
}
这个题有两种方法:DFS和BFS都可以解决
对于DFS来说,即深度优先搜索
对于题中的例子来说
我们首先遍历
public int closedIsland(int[][] grid) {
int row = grid.length;
int col = grid[0].length;
visited = new boolean[row][col];
int count = 0;
for(int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
if(grid[i][j] == 0 && !visited[i][j])
if (dfs(grid, i, j)) count++;
return count;
}
当我们遍历到“陆地”成功之后,就可以通过dfs,对上下左右四个方向进行深搜
当越界但仍是陆地时就返回false
if(curRow >= grid.length || curRow < 0 || curCol >= grid[0].length || curCol < 0) return false;
如果碰到海时就返回true
,当所有dfs,即所有子遍历都返回海(true
),才总的返回true
if(grid[curRow][curCol] == 1 || visited[curRow][curCol]) return true;
for(int[] direction : dir)
if(!dfs(grid, curRow + direction[0], curCol + direction[1])) flag = false;
同时,对已经遍历过的路径标记为已经遍历
visited[curRow][curCol] = true;
总的算法为:
private int[][] dir = new int[][]{{1,0},{-1,0},{0,-1},{0,1}};
private boolean[][] visited;
public int closedIsland(int[][] grid) {
int row = grid.length;
int col = grid[0].length;
visited = new boolean[row][col];
int count = 0;
for(int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
if(grid[i][j] == 0 && !visited[i][j])
if (dfs(grid, i, j)) count++;
return count;
}
private boolean dfs(int[][] grid, int curRow, int curCol){
if(curRow >= grid.length || curRow < 0 || curCol >= grid[0].length || curCol < 0) return false;
if(grid[curRow][curCol] == 1 || visited[curRow][curCol]) return true;
visited[curRow][curCol] = true;
boolean flag = true;
for(int[] direction : dir)
if(!dfs(grid, curRow + direction[0], curCol + direction[1])) flag = false;
return flag;
}