楼主: 白目先森
跳转到指定楼层
上一主题 下一主题
收起左侧

Amazon Intern Phone Interview Feb 4th 2pm PST

🔗
 楼主| 白目先森 2016-2-11 05:43:24 | 只看该作者
全局:
三塘木 发表于 2016-2-10 02:12
第二题lc好像有原题吧?只不过lc是四方向的貌似

嗯,后来别人告诉我查了一下的确是,似乎是一个word search还是啥的
回复

使用道具 举报

🔗
 楼主| 白目先森 2016-2-11 05:43:38 | 只看该作者
全局:
ntgd1102 发表于 2016-2-9 17:03
楼主, 这个题要用的BFS的话,你怎么标记啊visted的啊?

我就是非常鱼唇的开了一个数组……
回复

使用道具 举报

🔗
 楼主| 白目先森 2016-2-11 05:43:45 | 只看该作者
全局:
ntgd1102 发表于 2016-2-9 17:03
楼主, 这个题要用的BFS的话,你怎么标记啊visted的啊?

我就是非常鱼唇的开了一个数组……
回复

使用道具 举报

🔗
flashpacker 2016-2-11 09:02:31 | 只看该作者
全局:
我觉得直接DFS就可以了吧,不需要额外空间,每次用过的matrix位置改为一个其余字符
回复

使用道具 举报

🔗
ntgd1102 2016-2-11 11:20:06 | 只看该作者
全局:
其实这个题用BFS的话,就自己建一个costume class, 里面放个LinkedHashSet(如果让你输出没有不怎么走的话)就可以了。 周五电面,拜帖。
回复

使用道具 举报

🔗
ntgd1102 2016-2-11 11:21:34 | 只看该作者
全局:
flashpacker 发表于 2016-2-11 09:02
我觉得直接DFS就可以了吧,不需要额外空间,每次用过的matrix位置改为一个其余字符

就是用完再改回来就行了, 这个最intuitive了。
回复

使用道具 举报

🔗
aangel 2016-2-11 11:40:57 | 只看该作者
全局:
leetcode上有,word search,用DFS做的,
BFS怎么做啊
回复

使用道具 举报

🔗
aangel 2016-2-11 11:43:32 | 只看该作者
全局:
BFS会超时...
If you use BFS, you have to record the positions visited for each path, which can get Time Limit Exceeded.
回复

使用道具 举报

🔗
ntgd1102 2016-2-11 12:11:20 | 只看该作者
全局:
private static class Cell{
        int x;
        int y;
        Set<List<Integer>> path;
        private Cell(int x, int y) {
            this.x = x;
            this.y = y;
            this.path = new LinkedHashSet<>();
        }
    }
    public boolean findWord(char[][] matrix, String target) {
        if (matrix == null || matrix.length == 0
                || matrix[0].length == 0 || target == null || target.length() == 0) {
            return false;
        }

        int[][] dirs = new int[][] {{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1, -1},{1, -1},{-1, 1}};
        int row = matrix.length;
        int col = matrix[0].length;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (matrix[i][j] == target.charAt(0)) {
                    if (helper(matrix, i, j, dirs, target)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
    public boolean helper(char[][] matrix, int x, int y, int[][] dirs, String target) {
        int row = matrix.length;
        int col = matrix[0].length;
        int step = 0;
        Queue<Cell> queue = new LinkedList<>();
        Cell first = new Cell(x, y);
        first.path.add(Arrays.asList(x, y));
        queue.add(first); // 没加进去
        while (!queue.isEmpty()) {
            Cell currCell = queue.poll();
            step++;
            if (step == target.length()) {
                return true;
            }
            for (int[] dir : dirs) {
                int nextX = currCell.x + dir[0];
                int nextY = currCell.y + dir[1];
                List<Integer> nextPos = Arrays.asList(nextX, nextY);
                if (nextX >= 0 && nextX < row
                        && nextY >= 0 && nextY < col
                        && !currCell.path.contains(nextPos)
                        && matrix[nextX][nextY] == target.charAt(step)) {
                    Cell newCell = new Cell(nextX, nextY);
                    newCell.path = new LinkedHashSet<>(currCell.path);
                    newCell.path.add(nextPos);
                    queue.offer(newCell);
                }
            }
        }
        return false;
    }
回复

使用道具 举报

🔗
UCLA_andy 2016-2-13 04:32:34 | 只看该作者
全局:
想问下楼主有消息了么?
回复

使用道具 举报

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

本版积分规则

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