查看: 1258| 回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

[二分/排序/搜索] Island city 我的解法不对,百思不得其解

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
可以问一问我的解法为什么不对吗?
题目描述:Given a matrix of size n x m, the elements in the matrix are 0、1、2. 0 for the sea, 1 for the island, and 2 for the city on the island(You can assume that 2 is built on 1, ie 2 also represents the island).
If two 1 are adjacent, then these two 1 belong to the same island. Find the number of islands with at least one city.

就是在一个Matrix里寻找所有含有2的区域,返回符合条件的区域的数量。
我的思路是和number of island那题差不多。用DFS遍历每一个点,如果越界或者是0,或者已经被遍历过则返回false;如果是1的话继续遍历它的上下左右邻居,返回遍历的结果:如果邻居找到了2,那返回true;如果一个数据点本身就是2,那只需要遍历上下左右的邻居就可以了,返回true。
以下是我的代码:
  1. public class Solution {
  2.     /**
  3.      * @param grid: an integer matrix
  4.      * @return: an integer
  5.      */
  6.     private boolean islandCityFinder(int[][] grid, boolean[][] visited, int row, int col) {
  7.         if(row < 0 || row > grid.length - 1) {
  8.             return false;
  9.         }
  10.         if(col < 0 || col > grid[row].length - 1) {
  11.             return false;
  12.         }
  13.         if(visited[row][col] == true) {
  14.             return false;
  15.         }
  16.         visited[row][col] = true;
  17.         if(grid[row][col] == 0) {
  18.             return false;
  19.         }
  20.         else if(grid[row][col] == 1) {
  21.             return (islandCityFinder(grid, visited, row+1, col) ||
  22.                     islandCityFinder(grid, visited, row, col+1) ||
  23.                     islandCityFinder(grid, visited, row-1, col) ||
  24.                     islandCityFinder(grid, visited, row, col-1));
  25.         }
  26.         else {
  27.             //Case of 2
  28.             islandCityFinder(grid, visited, row+1, col);
  29.             islandCityFinder(grid, visited, row, col+1);
  30.             islandCityFinder(grid, visited, row-1, col);
  31.             islandCityFinder(grid, visited, row, col-1);
  32.             return true;
  33.         }
  34.     }
  35.     public int numIslandCities(int[][] grid) {
  36.         // Write your code here
  37.         if(grid.length == 0 || grid[0].length == 0) {
  38.             return 0;
  39.         }
  40.         boolean[][] visited = new boolean[grid.length][grid[0].length];
  41.         for(int i = 0; i < grid.length; i++) {
  42.             Arrays.fill(visited[i], false);
  43.         }
  44.         int count = 0;
  45.         for(int i = 0; i < grid.length; i++) {
  46.             for(int j = 0; j < grid[0].length; j++) {
  47.                 if(visited[i][j] == true) continue;
  48.                 if(islandCityFinder(grid, visited, i, j) == true) {
  49.                     count++;
  50.                 }
  51.             }
  52.         }
  53.         return count;
  54.     }
  55. }
复制代码

上一篇:利口 就留三
下一篇:Leetcode服务器挂了?才23号啊
推荐
忘记密码 2018-12-24 14:28:56 | 只看该作者
全局:
不是很熟悉java 但是||的用法应该和c++差不多吧
statement: s1 || s2 || s3 中,如果s1是true 那么程序根本不会计算s2和s3

这个例子
1 2
2 0
首先main的循环搜索到1 然后在dfs种从1搜索到下面的2 因为这个2会返回true 所以本次搜索中1的其他三个方向就不会被搜索了(除非从2有办法连过去) count++
然后main的循环搜索到第二个2 count++
会得出2个岛屿有岛的结论


而且if(visited[i][j] == true)感觉怪怪 java应该可以直接if(visited[i][j])?
另外搜索过的地方在原数组中直接赋值0就可以了 是不是不用专门那个建一个visited
回复

使用道具 举报

全局:
hi,楼主,看到您rpi的留言,想跟您请教一下rpi的问题,谢谢。能加我微信:lxy_9708,谢谢
回复

使用道具 举报

🔗
337845818 2019-4-9 03:49:29 | 只看该作者
全局:
对着每个2做floodfill 每次floodfill + 1
回复

使用道具 举报

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

本版积分规则

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