📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 2109| 回复: 7
跳转到指定楼层
上一主题 下一主题
收起左侧

Word Ladder

全局:

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

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

x
本帖最后由 love1point 于 2015-5-28 16:53 编辑

今天刷了Word Ladder, 参考了网上代码,但leetcode过不去,谁能帮帮忙,多谢哈
Submission Result: Compile Error
More Details


Line 20: error: incompatible types: Set<String> cannot be converted to HashSet<String>



public int ladderLength(String start, String end, HashSet<String> dict) {
if (dict.size() == 0)
return 0;
dict.add(end);
LinkedList<String> wordQueue = new LinkedList<String>();
LinkedList<Integer> distanceQueue = new LinkedList<Integer>();
wordQueue.add(start);
distanceQueue.add(1);
//track the shortest path
int result = Integer.MAX_VALUE;
while (!wordQueue.isEmpty()) {
String currWord = wordQueue.pop();
Integer currDistance = distanceQueue.pop();
if (currWord.equals(end)) {
result = Math.min(result, currDistance);
}
for (int i = 0; i < currWord.length(); i++) {
char[] currCharArr = currWord.toCharArray();
for (char c = ’a’; c <= ’z’; c++) {
currCharArr[i] = c;
String newWord = new String(currCharArr);
if (dict.contains(newWord)) {wordQueue.add(newWord);
distanceQueue.add(currDistance + 1);
dict.remove(newWord);
}
}
}
}
if (result < Integer.MAX_VALUE)
return result;
else
return 0;
}


上一篇:lintcode Find Peak Element II 一问
下一篇:Leetcode 刷题记录!!!不信自己今年找不到工作!!!
🔗
tailofjune 2015-5-28 17:47:21 | 只看该作者
全局:
不要乱改函数头.
回复

使用道具 举报

🔗
 楼主| love1point 2015-5-28 20:07:24 | 只看该作者
全局:

非常感谢,改对了
回复

使用道具 举报

🔗
 楼主| love1point 2015-5-28 20:07:49 | 只看该作者
全局:
AC 过了:



public class Solution {
    public int ladderLength(String beginWord, String endWord, Set<String> wordDict) {
        if (wordDict.size() == 0)
                return 0;

        wordDict.add(endWord);

        LinkedList<String> wordQueue = new LinkedList<String>();
        LinkedList<Integer> distanceQueue = new LinkedList<Integer>();

        wordQueue.add(beginWord);
        distanceQueue.add(1);

        //track the shortest path
        int result = Integer.MAX_VALUE;
        while (!wordQueue.isEmpty()) {
                String currWord = wordQueue.pop();
                Integer currDistance = distanceQueue.pop();

                if (currWord.equals(endWord)) {
                        result = Math.min(result, currDistance);
                }

                for (int i = 0; i < currWord.length(); i++) {
                        char[] currCharArr = currWord.toCharArray();
                        for (char c = 'a'; c <= 'z'; c++) {
                                currCharArr[i] = c;

                                String newWord = new String(currCharArr);
                                if (wordDict.contains(newWord)) {
                                        wordQueue.add(newWord);
                                        distanceQueue.add(currDistance + 1);
                                        wordDict.remove(newWord);
                                }
                        }
                }
        }

        if (result < Integer.MAX_VALUE)
                return result;
        else
                return 0;
    }
   
   
}


回复

使用道具 举报

🔗
whoami250 2015-6-21 07:27:56 | 只看该作者
全局:
堂堂快毕业的PhD抄答案都抄不对,真是佩服啊
这答案抄的,一个字母都不差,太厉害
https://github.com/adiggo/Leetcode/blob/master/WordLadder.java
回复

使用道具 举报

🔗
sharkwolf 2015-6-21 08:31:28 | 只看该作者
全局:
whoami250 发表于 2015-6-21 07:27
堂堂快毕业的PhD抄答案都抄不对,真是佩服啊
这答案抄的,一个字母都不差,太厉害
https://github.com/ad ...

打人不打脸,揭人不揭短, 厚道一点!
回复

使用道具 举报

🔗
whoami250 2015-6-21 11:02:00 | 只看该作者
全局:
sharkwolf 发表于 2015-6-21 08:31
打人不打脸,揭人不揭短, 厚道一点!

是我不厚道还是lz发的全是这种没有营养的帖子不厚道呢??
回复

使用道具 举报

🔗
sharkwolf 2015-6-21 13:39:52 | 只看该作者
全局:
whoami250 发表于 2015-6-21 11:02
是我不厚道还是lz发的全是这种没有营养的帖子不厚道呢??

来点幽默感, 轻松一下!
回复

使用道具 举报

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

本版积分规则

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