【问题标题】:Getting 73/115 testcases passed: Verifying an Alien dictionary获得 73/115 个测试用例通过:验证外星人字典
【发布时间】:2019-10-07 03:15:16
【问题描述】:

问。给定以外来语言书写的单词序列和字母顺序,当且仅当给定单词在该外来语言中按字典顺序排序时,才返回 true。 以下是一些例子:

Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
Output: true
Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.


Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
Output: false
Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.


Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
Output: false
Explanation: The first three characters "app" match, and the second string is shorter (in size.

以下是我的解决方案:

class Solution(object):
    def isAlienSorted(self, words, order):
        orderDict = {}
        for index, char in enumerate(order):
            orderDict[char] = index

        for j in range(len(words)-1):
            for i in range(min(len(words[j]),len(words[j+1]))):
                word1 = words[j]
                word2 = words[j+1]
                if orderDict[word1[i]] == orderDict[word2[i]]:
                    continue
                if orderDict[word1[i]] > orderDict[word2[i]]:
                    return False
                if orderDict[word1[i]] < orderDict[word2[i]]:
                    return True
            if len(words[j]) > len(words[j+1]):
                return False

        return True

为什么只有 73/115 个测试用例通过了?

【问题讨论】:

  • 是 Python 3 还是 Python 2?你已经标记了两者。
  • 不管怎样,return True 不好。您只确定了 words[j] 和 words[j + 1] 在这一点上的顺序正确,而不是其余的。相反,使用break 退出循环,并弄清楚如何跳过长度检查。 (Python 甚至有一种独特的方式来做到这一点。)
  • 你为什么要使用类来做到这一点?
  • @Ry- 在 python 中退出循环的独特方式是什么?
  • 不是退出循环,而是在使用break 退出时跳过长度检查。 for…else

标签: python data-structures testcase


【解决方案1】:

我找到了我的问题的答案。如果前一个单词的字符顺序小于该单词后一个单词的顺序,而不是返回 true,您应该使用“break”跳过这种情况。这可以防止程序返回误报,因为即使字典中前面有其他单词的顺序不正确,它也可能返回“true”:

def isAlienSorted(self, words, order):
        orderDict = {}
        for index, char in enumerate(order):
            orderDict[char] = index

        for j in range(len(words)-1):
            word1 = words[j]
            word2 = words[j+1]
            for i in range(min(len(word1),len(word2))):
                if orderDict[word1[i]] != orderDict[word2[i]]:
                    if orderDict[word1[i]] > orderDict[word2[i]]:
                        return False
                    break
                elif len(word1) > len(word2):
                    return False

        return True

此解决方案已被接受。

【讨论】:

    【解决方案2】:

    对您的算法进行了一些更改,请检查是否可行

        def isAlienSorted(self, words, order):
            orderDict = {}
            for index, char in enumerate(order):
                orderDict[char] = index
    
            for j in range(len(words)-1):
                isCheck = True
                for i in range(min(len(words[j]),len(words[j+1]))):
                    word1 = words[j]
                    word2 = words[j+1]
                    if orderDict[word1[i]] == orderDict[word2[i]]:
                        continue
                    if orderDict[word1[i]] > orderDict[word2[i]]:
                        return False
                    if orderDict[word1[i]] < orderDict[word2[i]]:
                        isCheck = False
                        break
                if isCheck and len(words[j]) > len(words[j+1]):
                    return False
    
            return True
    

    【讨论】:

      猜你喜欢
      • 2020-01-26
      • 1970-01-01
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多