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

🐶 店面

🔗
匿名用户-DORQL  2023-6-3 08:40:53 |倒序浏览

2023(1-3月) 码农类General 本科 全职@google - 猎头 - 技术电面  | 😃 Positive 😐 Average | Other | 在职跳槽

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

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

x
矩阵里面有很多 1 -1 0 然后把所有的1(可
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
点的坐标in a list

评分

参与人数 4大米 +9 收起 理由
shichsh07 + 2 欢迎分享你知道的情况,会给更多积分奖励!
Solumilken + 1 给你点个赞!
lychee817 + 1 给你点个赞!
匿名用户-CM6QR + 5

查看全部评分


上一篇:亚麻 OA
下一篇:Remitly 电话
地里匿名用户
推荐
匿名用户-DORQL  2023-6-10 13:01:36
Solumilken 发表于 2023-6-9 21:34
想問問樓主,
1. 連接起來的方式是上下左右四個方向的1都可連接嗎? 還是說可以 上+下+左+右 + 左上+左下+ ...

1 4个方向
2 对
3 把能连接的所有1都连起来 可能有多个1 所以连的路线可能有多条 路线也可以有重叠
回复

使用道具 举报

全局:
minimal spanning tree?
回复

使用道具 举报

推荐
oumizx 2023-7-4 15:32:14 | 只看该作者
全局:
  1. public class ConnectOne {

  2.     int[][] DIRS = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

  3.     public List<List<Integer>> solution(int[][] matrix) {
  4.         Set<Integer> resSet = new HashSet<>();
  5.         List<List<Integer>> res = new LinkedList<>();
  6.         boolean[][] visitedOne = new boolean[matrix.length][matrix[0].length];
  7.         for (int i = 0; i < matrix.length; i++) {
  8.             for (int j = 0; j < matrix[0].length; j++) {
  9.                 if (matrix[i][j] == 1 && !visitedOne[i][j]) {
  10.                     helper(resSet, matrix, i, j, new boolean[matrix.length][matrix[0].length], visitedOne, 0);
  11.                 }
  12.             }
  13.         }

  14.         for (int idx : resSet) {
  15.             res.add(Arrays.asList(idx / matrix[0].length, idx % matrix[0].length));
  16.         }

  17.         return res;
  18.     }

  19.     private void helper(Set<Integer> resSet, int[][] matrix, int row, int col, boolean[][] curVisited, boolean[][] visitedOne, int len) {
  20.         if (row < 0 || row >= matrix.length || col < 0 || col >= matrix[0].length || matrix[row][col] == -1 || curVisited[row][col]) {
  21.             return;
  22.         }

  23.         resSet.add(row * matrix[0].length + col);
  24.         if (matrix[row][col] == 1) {
  25.             visitedOne[row][col] = true;
  26.             if (len != 0) {
  27.                 return;
  28.             }
  29.         }

  30.         curVisited[row][col] = true;
  31.         for (int[] dir : DIRS) {
  32.             int newRow = row + dir[0];
  33.             int newCol = col + dir[1];
  34.             helper(resSet, matrix, newRow, newCol, curVisited, visitedOne, len + 1);
  35.         }
  36.         curVisited[row][col] = false;
  37.     }

  38.     public static void main(String[] args) {
  39.         ConnectOne solution = new ConnectOne();
  40.         int[][] matrix = {{1, 0, -1, -1, 1}, {0, 0, 1, -1, 0}, {0, 1, -1, -1, 0}, {-1, -1, -1, -1, 1}};
  41.         List<List<Integer>> resSet = solution.solution(matrix);
  42.         int[][] printMatrix = new int[matrix.length][matrix[0].length];
  43.         for (List<Integer> list : resSet) {
  44.             printMatrix[list.get(0)][list.get(1)] = 1;
  45.         }
  46.         for (int i = 0; i < printMatrix.length; i++) {
  47.             for (int j = 0; j < printMatrix[0].length; j++) {
  48.                 System.out.print(printMatrix[i][j] + ",");
  49.             }
  50.             System.out.println();
  51.         }
  52.     }
  53. }
复制代码
回复

使用道具 举报

全局:
可以revisit坐标吗
回复

使用道具 举报

🔗
slowloris 2023-6-5 14:06:27 | 只看该作者
全局:
应该就是个dfs
回复

使用道具 举报

🔗
x24yang 2023-6-6 01:35:50 | 只看该作者
全局:
从所有1开始bfs就可以。没有follow up吗?
回复

使用道具 举报

地里匿名用户
🔗
匿名用户-DBTTX  2023-6-6 07:06:38 来自APP
x24yang 发表于 2023-06-05 10:35:50
从所有1开始bfs就可以。没有follow up吗?
请问bfs怎么判断1全部连接了
回复

使用道具 举报

🔗
x24yang 2023-6-6 08:36:09 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
Solumilken 2023-6-10 12:34:42 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
orochileo 2023-6-11 14:19:21 | 只看该作者
全局:
UnionFind能不能做呀
回复

使用道具 举报

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

本版积分规则

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