亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么字符串按字母順序排序?

為什么字符串按字母順序排序?

Cats萌萌 2023-06-20 16:48:59
這是來自 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]))輸出:['--...-.', '.-.--..', '--.--...', '--.--...']    
查看完整描述

2 回答

?
慕森王

TA貢獻1777條經驗 獲得超3個贊

問題是您首先遍歷 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 的實現方式,但它是對您的解決方案的最小修復。


就個人而言,我會使用字典將字母映射到摩爾斯電碼,然后遍歷字符串中的字符。

查看完整回答
反對 回復 2023-06-20
?
猛跑小豬

TA貢獻1858條經驗 獲得超8個贊

不確定您面臨的問題,但這會過去:


class Solution:

    def uniqueMorseRepresentations(self, words):

        morse_map = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",

             "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]

        return len({''.join(morse_map[ord(char) - 97] for char in word) for word in words})

97 是ord('a'):


class Solution:

    def uniqueMorseRepresentations(self, words):

        morse_map = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",

             "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]

        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 {

    public static final int uniqueMorseRepresentations(

        final String[] words

    ) {

        final String[] morseMap = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};

        Set<String> transformations = new HashSet<>();


        for (String word : words) {

            StringBuilder transformation = new StringBuilder();


            for (int index = 0; index < word.length(); index++)

                transformation.append(morseMap[word.charAt(index) - 97]);


            transformations.add(transformation.toString());

        }


        return transformations.size();

    }

}


查看完整回答
反對 回復 2023-06-20
  • 2 回答
  • 0 關注
  • 174 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號