楼主: zhangrz2
跳转到指定楼层
上一主题 下一主题
收起左侧

多伦多在职每日刷题打卡也欢迎组队

🔗
 楼主| zhangrz2 2019-9-1 09:59:13 | 只看该作者
全局:
417. Pacific Atlantic Water Flow

  1. public class Solution {
  2.     int[][]dir = new int[][]{{1,0},{-1,0},{0,1},{0,-1}};
  3.     public List<List<Integer>> pacificAtlantic(int[][] matrix) {
  4.         List<List<Integer>> res = new ArrayList<>();
  5.         if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
  6.             return res;
  7.         }
  8.         int n = matrix.length, m = matrix[0].length;
  9.         //One visited map for each ocean
  10.         boolean[][] pacific = new boolean[n][m];
  11.         boolean[][] atlantic = new boolean[n][m];
  12.         Queue<int[]> pQueue = new LinkedList<>();
  13.         Queue<int[]> aQueue = new LinkedList<>();
  14.         for(int i=0; i<n; i++){ //Vertical border
  15.             pQueue.offer(new int[]{i, 0});
  16.             aQueue.offer(new int[]{i, m-1});
  17.             pacific[i][0] = true;
  18.             atlantic[i][m-1] = true;
  19.         }
  20.         for(int i=0; i<m; i++){ //Horizontal border
  21.             pQueue.offer(new int[]{0, i});
  22.             aQueue.offer(new int[]{n-1, i});
  23.             pacific[0][i] = true;
  24.             atlantic[n-1][i] = true;
  25.         }
  26.         bfs(matrix, pQueue, pacific);
  27.         bfs(matrix, aQueue, atlantic);
  28.         for(int i=0; i<n; i++){
  29.             for(int j=0; j<m; j++){
  30.                 if(pacific[i][j] && atlantic[i][j])
  31.                     res.add(Arrays.asList(new Integer[]{i,j}));
  32.             }
  33.         }
  34.         return res;
  35.     }
  36.     public void bfs(int[][]matrix, Queue<int[]> queue, boolean[][]visited){
  37.         int n = matrix.length, m = matrix[0].length;
  38.         while(!queue.isEmpty()){
  39.             int[] cur = queue.poll();
  40.             for(int[] d:dir){
  41.                 int x = cur[0]+d[0];
  42.                 int y = cur[1]+d[1];
  43.                 if(x<0 || x>=n || y<0 || y>=m || visited[x][y] || matrix[x][y] < matrix[cur[0]][cur[1]]){
  44.                     continue;
  45.                 }
  46.                 visited[x][y] = true;
  47.                 queue.offer(new int[]{x, y});
  48.             }
  49.         }
  50.     }
  51. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-9-1 22:13:37 | 只看该作者
全局:
1099. Two Sum Less Than K

  1. class Solution {
  2.     public int twoSumLessThanK(int[] A, int K) {
  3.         TreeSet<Integer> set = new TreeSet<>();
  4.         int res = -1;
  5.         for (int a : A) {
  6.             Integer pre = set.lower(K - a);
  7.             if (pre != null) {
  8.                 res = Math.max(res, a + pre);
  9.             }
  10.             set.add(a);
  11.         }
  12.         return res;
  13.     }
  14. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表