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

[Leetcode] Word Ladder BFS总是过不了大数据是为何?求指点!

全局:

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

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

x
public class Solution {       
        List<String> ret ;
         
         public int ladderLength(String beginWord, String endWord, Set<String> wordList) {
                 ret = new ArrayList<>();
                 List<List<String>> paths = new ArrayList<List<String>>();
                 List<String> path = new ArrayList<String>();
                 path.add(beginWord);
                 paths.add(path);
                 bfs(paths, wordList, endWord);
                 return ret.size();
         }
         public void bfs(List<List<String>> paths ,Set<String> wordList,String target){
                if(paths.size() == 0) {
                        return;
                }
                Iterator<List<String>> itr = paths.iterator();
                List<List<String>> pathsCopy = new ArrayList<List<String>>(paths);
                while(itr.hasNext()){
                        List<String> tempPath = itr.next();
                        pathsCopy.remove(tempPath);
                        String tempS = tempPath.get(tempPath.size()-1);
                        for (int i = 0; i < tempS.length(); i++) {
                                for (int j = 0; j < 25; j++) {
                                        char temp = (char) ('a' + j);
                                        if(tempS.charAt(i) != temp){
                                                String nString =tempS.substring(0, i) + temp + tempS.substring(i+1);
                                                if(wordList.contains(nString)) {
                                                        List<String> currPath = new ArrayList<String>(tempPath);
                                                        currPath.add(nString);
                                                        wordList.remove(nString);
                                                        if(nString.equals(target)){
                                                                ret = currPath;
                                                                return;
                                                        }
                                                        pathsCopy.add(currPath);
                                                }
                                        }
                                }
                        }
                }
                bfs(pathsCopy, wordList, target);
         }
}

上一篇:Leetcode各公司列表题目合集
下一篇:LeetCode Premium Subscription 合买
🔗
 楼主| liuwujijay 2016-4-9 00:14:42 | 只看该作者
全局:
是不是因为 List<String> currPath = new ArrayList<String>(tempPath); 是一个O(N)的操作?
回复

使用道具 举报

🔗
xianming 2016-4-10 05:56:12 | 只看该作者
全局:
我刷这题时,当时也有个大数据总是报内存不够。我是用C++刷的,后来把参数改为by reference,加了个&,就过了。这个经验不知对楼主是否有帮助。
回复

使用道具 举报

🔗
ykwwind 2016-4-10 08:18:00 | 只看该作者
全局:
String的构造改成stringbuilder
回复

使用道具 举报

🔗
sevenyunan 2016-4-10 08:22:51 | 只看该作者
全局:
可以用dfs吗
回复

使用道具 举报

🔗
zdhzh05 2016-4-10 15:33:23 | 只看该作者
全局:
这题不是word ladder II,bfs不用记录每条路径(这里会浪费时间,数据量大的话还会内存溢出),每遍历一层未访问节点,层数(距离)自增,直到找到目标节点,或bfs结束(未找到目标节点)
回复

使用道具 举报

🔗
zdhzh05 2016-4-10 15:41:11 | 只看该作者
全局:

应该不行,bfs只要找到目标就可以了,但dfs要搜索每条路径,不断更新到目标的距离(能到达目标的话),才能确保找到目标的距离是最小的
回复

使用道具 举报

🔗
xperzy 2016-4-13 01:57:35 | 只看该作者
全局:
双向宽度优先应该是可以搞定的吧

我的blog里有相关:http://yucoding.blogspot.com/2014/01/leetcode-question-word-ladder-ii.html
回复

使用道具 举报

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

本版积分规则

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