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

[Leetcode] 求帮忙关于:Number of island variant question

全局:

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

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

x
如下, 有没有好心人给个解题思路 TT
Input: 2D array, x coordinate, y coordinate

Output: number of adjacent element that is same as the arr[x Coordinate][y coordinate]

For example,
array:
[7,7,1,1]
[1,0,0,0]
[0,0,0,7]
[0,0,7,7]

x = 3
y = 3

Then array[3][3] = 7 which output should be 3 because
[7,7,1,1]
[1,0,0,0]
[0,0,0,7]
[0,0,7,7]
these three sevens.

评分

参与人数 1大米 +1 收起 理由
14417335 + 1 给你点个赞!

查看全部评分


上一篇:GuideWire考试
下一篇:LC 上 Amazon tag所有题按freq降序
全局:
是问给定坐标下的7在内周围连着的7吗?那从这个点出发dfs+visited set就可以吧?
回复

使用道具 举报

🔗
 楼主| 微信用户_edc6079 2022-8-24 08:24:27 | 只看该作者
全局:
本帖最后由 微信用户_edc6079 于 2022-8-23 20:28 编辑
Dearkano 发表于 2022-8-23 17:46
是问给定坐标下的7在内周围连着的7吗?那从这个点出发dfs+visited set就可以吧?

是的, array[3][3], which both 3 are from input, xCoordinate and yCoordinate. 我试着用bfs来做但是还是无法implement
回复

使用道具 举报

全局:
微信用户_edc6079 发表于 2022-08-23 17:24:27
是的, array, which both 3 are from input, xCoordinate and yCoordinate. 我试着用bfs来做但是还是无法implement
为啥不能呢?visited set记录走过的地方,每从queue拿出一个点就向四个方向探索,如果是7并且不在visited里就入队并且res++
回复

使用道具 举报

🔗
 楼主| 微信用户_edc6079 2022-8-24 08:46:18 | 只看该作者
全局:
Dearkano 发表于 2022-8-23 20:42
为啥不能呢?visited set记录走过的地方,每从queue拿出一个点就向四个方向探索,如果是7并且不在visited ...

是的,我就是按你说的这样做的,我不知道为什么我的output不正确,可能我太菜了,帮帮我吧呜呜
回复

使用道具 举报

全局:
能不能把有问题的代码贴上来,可以帮你看看
回复

使用道具 举报

🔗
 楼主| 微信用户_edc6079 2022-8-24 21:26:47 | 只看该作者
全局:
AppleFans 发表于 2022-8-24 02:38
能不能把有问题的代码贴上来,可以帮你看看
  1. public static class location{
  2.         int row;
  3.         int col;
  4.         public location(int row, int col){
  5.             this.row = row;
  6.             this.col = col;
  7.         }
  8.     }
  9.     public static boolean isValid(Integer inputMap[][] , boolean visited[][], int currentRowLocation, int currentColLocation, int target){

  10.         if ((currentRowLocation >= 0 && currentRowLocation < visited.length) &&
  11.                 (currentColLocation >= 0 && currentColLocation < visited[0].length) &&
  12.                 inputMap[currentRowLocation][currentColLocation] == target &&
  13.                 !visited[currentRowLocation][currentColLocation]){
  14.             return true;
  15.         }else {
  16.             return  false;
  17.         }

  18.     }
  19.     public static void BreathFirstSearch(Integer[][] inputMap, boolean visited[][], int rowLocation, int colLocation){

  20.         int row = inputMap.length;
  21.         int col = inputMap[0].length;
  22.         int target = inputMap[rowLocation][colLocation];
  23.         Queue<location> queueForLocation = new LinkedList<>();
  24.         queueForLocation.add(new location(rowLocation,colLocation));
  25.         visited[rowLocation][colLocation] = true;

  26.         while(!queueForLocation.isEmpty()){
  27.             location currentLocation =  queueForLocation.peek();
  28.             int x = currentLocation.row;
  29.             int y = currentLocation.col;
  30.             queueForLocation.remove();

  31. //            System.out.println(inputMap[x][y] + " ");
  32.             for(int i = 0; i < 4; i++){
  33.                 int adjXCoordinate = x + i;
  34.                 int adjYCoordinate = y + i;

  35.                 if(isValid(inputMap, visited, adjXCoordinate, adjYCoordinate, target)){
  36.                     queueForLocation.add(new location(adjXCoordinate, adjYCoordinate));
  37.                     System.out.println(inputMap[adjXCoordinate][adjYCoordinate]);
  38.                     visited[adjXCoordinate][adjYCoordinate] = true;

  39.                 }
  40.             }
  41.         }
  42.     }

  43. // First testing input:
  44.         // [1,2,3]
  45.         // [4,5,6]
  46.         // [2,1,1]
  47.         // target = input[2][2] = 2
  48.         // expected output = 4 -->


复制代码
感谢啊
回复

使用道具 举报

🔗
AppleFans 2022-8-25 03:35:25 | 只看该作者
全局:

line 38 39, 为什么要 x + i, y + i? 你的i是从0到3,这样也没有访问上下左右四个neighbor啊。

上:[-1, 0] 下:[1, 0] 左:[0, -1] 右:[0, 1]
回复

使用道具 举报

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

本版积分规则

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