📣 Back to School开学季 - VIP通行证5折优惠!蓝莓、Offer多多同步优惠
查看: 1112| 回复: 5
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] Leetcode 269 题BFS方法有一个test case有问题

全局:

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

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

x
哈喽大家,今天刷到269 Alien Dictionary 的时候, 用的BFS 方法,但是发现有个test case很奇怪,["ba", "bc"], 跑下来结果是"abc", 但是理想答案应该是"bac" 才对啊, 希望有明白的同学能给解答解答!!
  1. class Solution {
  2.     public String alienOrder(String[] words) {
  3.         int[] degree = new int[26];
  4.         int count = 0;
  5.         StringBuilder res = new StringBuilder();
  6.         
  7.         for (String word : words) {
  8.             for (char c : word.toCharArray()) {
  9.                 if (degree[c - 'a'] == 0) {
  10.                     degree[c - 'a'] = 1;
  11.                     count++;
  12.                 }
  13.             }
  14.         }
  15.         
  16.         HashMap<Character, Set<Character>> map = new HashMap<>();
  17.         
  18.         for (int i = 0; i < words.length - 1; i++) {
  19.             int len = Math.min(words[i].length(), words[i + 1].length());
  20.             for (int j = 0; j < len; j++) {
  21.                 char cur = words[i].charAt(j);
  22.                 char next = words[i + 1].charAt(j);
  23.                 if (cur != next) {
  24.                     if (!map.containsKey(cur)) {
  25.                         map.put(cur, new HashSet<>());
  26.                     }
  27.                     if (map.get(cur).add(next)) {
  28.                         degree[next - 'a']++;
  29.                     }
  30.                     break;
  31.                 }
  32.                 if (j == words[i + 1].length() - 1 && words[i + 1].length() < words[i].length()) {
  33.                     return "";
  34.                 }
  35.             }
  36.         }
  37.         
  38.         Queue<Character> queue = new LinkedList<>();
  39.         for (int i = 0; i < 26; i++) {
  40.             if (degree[i] == 1) {
  41.                 queue.offer((char)(i + 'a'));
  42.             }
  43.         }
  44.         
  45.         while (!queue.isEmpty()) {
  46.             Character c = queue.poll();
  47.             res.append(c);
  48.             if (map.containsKey(c)) {
  49.                 for (char ch : map.get(c)) {
  50.                     if (--degree[ch - 'a'] == 1) {
  51.                         queue.offer(ch);
  52.                     }
  53.                 }
  54.             }
  55.         }
  56.         
  57.         if (res.length() != count) return "";
  58.         
  59.         return res.toString();
  60.     }
  61. }
复制代码



上一篇:求解|地铁迷问题
下一篇:刷题相关疑问
全局:
premium题 锁了看不了..
回复

使用道具 举报

🔗
 楼主| fish1994 2020-11-3 03:51:55 | 只看该作者
全局:
一剑终情 发表于 2020-11-3 03:10
premium题 锁了看不了..

不好意思啊, 我把题目要求贴上去了
回复

使用道具 举报

🔗
 楼主| fish1994 2020-11-3 03:52:21 | 只看该作者
全局:
题目要求在此字数字数字数

image.png (32.18 KB, 下载次数: 0)

image.png
回复

使用道具 举报

全局:
题目只代表a在c前面,b在任何位置都可以

评分

参与人数 1大米 +1 收起 理由
fish1994 + 1 很有用的信息!

查看全部评分

回复

使用道具 举报

全局:
如果test case错了你可以给Leetcode举报,他们采纳的话会给你发coin
回复

使用道具 举报

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

本版积分规则

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