为什么字符串按字母顺序排序?

这是来自 Leetcode 804:唯一的摩尔斯密码词。我想知道为什么我的代码给出了正确的摩尔斯电码,但它是按字母顺序排序的,这不是故意的。任何贡献表示赞赏。


输入:


words = ["gin", "zen", "gig", "msg"]

代码:


class Solution:

    def uniqueMorseRepresentations(self, words: List[str]) -> int:

        morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

        alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

        transformation = []

        zip_ = list(zip(morse, alphabet))

        for word in words:

            transformation.append(''.join(code[0] for code in zip_ for letter in word if letter in code[1]))

输出:


['--...-.', '.-.--..', '--.--...', '--.--...']    


Cats萌萌
浏览 104回答 2
2回答

慕森王

问题是您首先遍历 zip_,然后遍历字母。这就是导致字母顺序的原因—— zip_ 按字母顺序排序。这个版本做你想让它做的事: class Solution:       def uniqueMorseRepresentations(self, words: List[str]) -> int:           morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]           alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']           transformation = []           zip_ = list(zip(morse, alphabet))           for word in words:               transformation.append(''.join(code[0] for letter in word for code in zip_  if letter in code[1]))这不是最 Pythonic 的实现方式,但它是对您的解决方案的最小修复。就个人而言,我会使用字典将字母映射到摩尔斯电码,然后遍历字符串中的字符。

猛跑小猪

不确定您面临的问题,但这会过去:class Solution:&nbsp; &nbsp; def uniqueMorseRepresentations(self, words):&nbsp; &nbsp; &nbsp; &nbsp; morse_map = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]&nbsp; &nbsp; &nbsp; &nbsp; return len({''.join(morse_map[ord(char) - 97] for char in word) for word in words})97 是ord('a'):class Solution:&nbsp; &nbsp; def uniqueMorseRepresentations(self, words):&nbsp; &nbsp; &nbsp; &nbsp; morse_map = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]&nbsp; &nbsp; &nbsp; &nbsp; return len({''.join(morse_map[ord(char) - ord('a')] for char in word) for word in words})我没有在您的解决方案中看到return声明或 a set()。有两个简单的步骤:将访问过的转换添加到集合中返回集合的长度set()如果您有兴趣,这里还有一个使用 HashSet 的 Java 版本(类似于Python 中的):public final class Solution {&nbsp; &nbsp; public static final int uniqueMorseRepresentations(&nbsp; &nbsp; &nbsp; &nbsp; final String[] words&nbsp; &nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; final String[] morseMap = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};&nbsp; &nbsp; &nbsp; &nbsp; Set<String> transformations = new HashSet<>();&nbsp; &nbsp; &nbsp; &nbsp; for (String word : words) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StringBuilder transformation = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int index = 0; index < word.length(); index++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transformation.append(morseMap[word.charAt(index) - 97]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transformations.add(transformation.toString());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return transformations.size();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python