java面试题(7)
来源:互联网 发布:oracle sql 去掉重复 编辑:程序博客网 时间:2024/06/06 12:41
原题:
// Given an m x n matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches the right and bottom edges.// Water can only flow in four directions (up, down, left, or right) from a cell to another one with height equal or lower.// Find the list of grid coordinates where water can flow to both the Pacific and Atlantic ocean.// Note: // The order of returned grid coordinates does not matter. // Both m and n are less than 150.// Example:// Given the following 5x5 matrix:// Pacific ~ ~ ~ ~ ~ // ~ 1 2 2 3 (5) *// ~ 3 2 3 (4) (4) *// ~ 2 4 (5) 3 1 *// ~ (6) (7) 1 4 5 *// ~ (5) 1 1 2 4 *// * * * * * Atlantic// Return:// [[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with parentheses in above matrix).
答案:
public class Solution { public List<int[]> pacificAtlantic(int[][] matrix) { List<int[]> result = new LinkedList<>(); //error checking if(matrix == null || matrix.length == 0 || matrix[0].length == 0) { return result; } int n = matrix.length; int m = matrix[0].length; boolean[][] pacific = new boolean[n][m]; boolean[][] atlantic = new boolean[n][m]; for(int i = 0; i < n; i++) { dfs(matrix, pacific, Integer.MIN_VALUE, i, 0); dfs(matrix, atlantic, Integer.MIN_VALUE, i, m - 1); } for(int i = 0; i < m; i++) { dfs(matrix, pacific, Integer.MIN_VALUE, 0, i); dfs(matrix, atlantic, Integer.MIN_VALUE, n - 1, i); } for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(pacific[i][j] && atlantic[i][j]) { result.add(new int[] {i, j}); } } } return result; } public void dfs(int[][] matrix, boolean[][] visited, int height, int x, int y) { int n = matrix.length; int m = matrix[0].length; if(x < 0 || x >= n || y < 0 || y >= m || visited[x][y] || matrix[x][y] < height) { return; } visited[x][y] = true; dfs(matrix, visited, matrix[x][y], x + 1, y); dfs(matrix, visited, matrix[x][y], x - 1, y); dfs(matrix, visited, matrix[x][y], x, y + 1); dfs(matrix, visited, matrix[x][y], x, y - 1); }}
阅读全文
0 0
- Java面试题(7)
- java面试题(7)
- java面试题7
- 面试题(Java)
- JAVA面试题最全集(7)
- Java面试题全集(7)
- Java面试题全集(7)
- Java面试题集--Java面试题(答案)
- java面试题系列7
- Java面试题(答案)-
- 面试题(三)Java
- 面试题(四)Java
- 面试题(六)Java
- 面试题(七)JAVA
- 面试题(九)JAVA
- JAVA面试题(一)
- Java面试题(一)
- Java面试题(2)
- 初学Android之Tablayout+Fragment+ImageLoader(低仿知乎)
- java面试题(5)
- ant+findbugs 扫描代码生成报告
- java面试题(6)
- POJ1062->最短路Dij
- java面试题(7)
- java面试题(8)
- Apply与call理解与用法
- 【正一专栏】喜迎十九大——朝鲜会放烟花不?
- Leetcode题目汇总-Java实现
- java面试题(9)
- jQuery easyUI中表格的跨行跨列
- PKI系统介绍
- response详解