12
返回列表 发新帖
楼主: 匿名
跳转到指定楼层
上一主题 下一主题
收起左侧

🐶 店面

🔗
TerryK 2023-6-11 21:14:58 来自APP | 只看该作者
全局:
很簡單和典型的graph題了其實 無論哪一種,幾乎都是dfs+set,或者dfs+backtrack(通常是給終點起點然後找所有可能路線),o不overflow的看你recursion是tail還是non tail recursion,幾乎都不存在大問題
回复

使用道具 举报

🔗
chaoye 2023-6-16 10:34:59 | 只看该作者
全局:
TerryK 发表于 2023-6-11 09:14
很簡單和典型的graph題了其實 無論哪一種,幾乎都是dfs+set,或者dfs+backtrack(通常是給終點起點然後找所 ...

dfs你怎么做到尾递归??
回复

使用道具 举报

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

使用道具 举报

🔗
chaoye 2023-6-16 11:21:55 | 只看该作者
全局:
TerryK 发表于 2023-6-15 22:55
加個stack,類似

def preOrderTraversal(node):

加个stack只是把递归的调用stack移到自己定义的stack,该overflow还是会overflow,你这样也没解决这个问题。
回复

使用道具 举报

全局:
x24yang 发表于 2023-6-5 10:35
从所有1开始bfs就可以。没有follow up吗?

bfs怎么记录path啊?如果path有0但最后没有1的话那这些0会被记录但应该是不需要的吧
回复

使用道具 举报

🔗
x24yang 2023-6-28 10:50:06 | 只看该作者
全局:
呆呆大师兄 发表于 2023-6-27 17:05
bfs怎么记录path啊?如果path有0但最后没有1的话那这些0会被记录但应该是不需要的吧

这个不就是build post office吗?没有要记录path,要把所有经过的0都记录下来,进queue一个就记录一个。
回复

使用道具 举报

🔗
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. }
复制代码
回复

使用道具 举报

地里匿名用户
🔗
匿名用户-XXXQX  2023-10-12 21:30:40
标准BFS。有可能不是完全联通的,因此要遍历所有为1的点。此外,BFS的时候还需要保存当前的路径。
如果要求最短,可以先用BFS构建一个heuristic矩阵。具体来说,就是某个点离其最近的1的距离。然后用Dijkstra搜索最短路径
回复

使用道具 举报

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

本版积分规则

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