📣 独立日限时特惠: VIP通行证立减$68
楼主: jyty
跳转到指定楼层
上一主题 下一主题
收起左侧

word ladder 1.5 如果被challenge用26个char iteration太慢的话,大家怎么optimize?

🔗
bych0223 2016-12-10 12:40:49 | 只看该作者
本楼:
全局:
同关注+1
回复

使用道具 举报

🔗
say543 2016-12-10 15:31:12 | 只看该作者
全局:
ausgoo 发表于 2016-12-10 02:29
回答twoEnd BFS? 在leetcode discuss上有详解。 我自己试了下,快很多。谢谢。

two way bfs 不知道面试官给不给用 a-z 的enumeration...
回复

使用道具 举报

🔗
smallwarm 2016-12-11 14:31:11 | 只看该作者
全局:
RE .... 感觉这个非常值得讨论, 同关注!! +1
回复

使用道具 举报

全局:
solarsys 发表于 2016-11-29 03:23
trieNode 接hashmap  hashmap 的value 继续是trieNode 这样每次获取只有O1

这也不是O(1)吧, worst case还是每次都是O(26)?
回复

使用道具 举报

🔗
yangluphil 2016-12-19 07:24:39 | 只看该作者
全局:
可以预处理得到一个graph,每个单词是一个graph node,(V1, V2) is in Edges iff diff(V1, V2) == 1
回复

使用道具 举报

🔗
solarsys 2016-12-20 06:12:19 | 只看该作者
全局:
最近都没上自己的账号 不过仔细想了想好像hashmap的方法好像是自己误入歧途了。。大家请无视我的误导
回复

使用道具 举报

蓝蓝天了 该用户已被删除
🔗
蓝蓝天了 2017-3-9 12:10:09 | 只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

全局:
贴个我自己polish过的版本,
   while 处理queue时不用再遍历size.
                //int len = queue.size();
                //for (int i = 0; i < len; i++) {

Map<String, Set<String>> oneCharDiffWords = new HashMap<>();
        Map<String, String> ancestorMap = new HashMap<>();         
        public List<String> ladderLength(String start, String end, List<String> words) {
            List<String> list = new ArrayList<>(words);
            if (start.equals(end))
                return null;
            list.add(start);
            list.add(end);
            for (String S : list) {
                 Set<String> set = new HashSet<>();
                 for (String T : list)
                     if (!S.equals(T) && isOneCharDiff(S, T))
                         set.add(T);
               
                 oneCharDiffWords.put(S, set);
            }
            Queue<String> queue = new LinkedList<>();
            Set<String> seen = new HashSet<>();
            queue.offer(start);
            seen.add(start);
            while (!queue.isEmpty()) {
                //int len = queue.size();
                //for (int i = 0; i < len; i++) {
                    String cur = queue.poll();
                    Set<String> oneCharDiffWordsSet = oneCharDiffWords.get(cur);
                    for (String str : oneCharDiffWordsSet) {
                        if (seen.add(str)){
                                ancestorMap.put(str, cur);
                            queue.offer(str);
                        }
                        if (str.equals(end)) return CreatePathFromAncestorMap(str);
                    }
                //}
            }
            return new ArrayList<String>();
        }

        private List<String> CreatePathFromAncestorMap(String end){
            List<String> res = new ArrayList<>();
            String ancestor=end;
            while(ancestor!=null){
                res.add(0, ancestor);
                ancestor=ancestorMap.get(ancestor);
            }
            return res;
        }

        private boolean isOneCharDiff(String a, String b) {
            int diff = 0;
            for (int i = 0; i < a.length(); i++)
                if (a.charAt(i) != b.charAt(i))
                    diff++;
            
            return diff == 1;
        }

        public static void main(String[] args) {
                WordLadder1Dot5Way sol = new WordLadder1Dot5Way();
            List<String> res= sol.ladderLength("hit", "lib", new ArrayList<String>(Arrays.asList("cit", "cib", "lib", "hib")));
            for(String s: res) System.out.print(" "+ s);
        }
回复

使用道具 举报

🔗
jy_121 2017-10-24 12:42:13 | 只看该作者
本楼:
全局:
关注一下
回复

使用道具 举报

🔗
knight0clk 2017-10-30 07:38:05 | 只看该作者
全局:
春山寒 发表于 2016-11-28 11:32
是不是可以用trie来做 在每个position 只变化相同level trie里面有的字母

应该是用这个方法,但是感觉速度也不会提高很多
回复

使用道具 举报

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

本版积分规则

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