摘要:題目自己的解決方法其他解決方法
1.題目
International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on.
For convenience, the full table for the 26 letters of the English alphabet is given below:
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, "cba" can be written as "-.-..--...", (which is the concatenation "-.-." + "-..." + ".-"). We"ll call such a concatenation, the transformation of a word.
Return the number of different transformations among all words we have.
Example: Input: words = ["gin", "zen", "gig", "msg"] Output: 2 Explanation: The transformation of each word is: "gin" -> "--...-." "zen" -> "--...-." "gig" -> "--...--." "msg" -> "--...--." There are 2 different transformations, "--...-." and "--...--.".
自己的解決方法
class Solution: def uniqueMorseRepresentations(self, words: List[str]) -> int: final_set = set() mosLIst = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] for word in words: temp = "" for i in word: temp += mosLIst[ord(i)-97] final_set.add(temp) return len(final_set)
Runtime: 36 ms, faster than 97.45% of Python3 online submissions for Unique Morse Code Words. Memory Usage: 12.9 MB, less than 5.36% of Python3 online submissions for Unique Morse Code Words.
3.其他解決方法
const codes = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] const getIdx = char => char.charCodeAt(0) - "a".charCodeAt(0) var uniqueMorseRepresentations = function(words) { return words.map( word => word.split("") .map( char => codes[getIdx(char)]) .join("")) .reduce((set, cur) => set.add(cur), new Set()) .size };
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/43449.html
摘要:題目自己的解決方法其他解決方法 1.題目International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: a maps to .-, b maps to -..., c maps to -.-., and...
摘要:中文摩斯密碼是一個使用編寫的用于瀏覽器和環境的摩斯密碼庫,可以支持,即支持中文的摩斯密碼解密和加密。安裝引入使用只有兩個,名字為對于例子如下越過長城,走向世界對于例子如下相關地址在線開源地址 中文摩斯密碼 - Xmorse Xmorse?是一個使用 Javascript 編寫的用于瀏覽器和 nodejs 環境的摩斯密碼庫,可以支持 unicode,即支持中文的摩斯密碼解密和加密。 1...
摘要:安全透明輕量的螞蟻區塊鏈摩斯安全計算基礎設施螞蟻摩斯依托螞蟻金融科技平臺,結合區塊鏈技術,將復雜的隱私保護與密碼學算法透明化產品化,提供安全發布安全模型安全統計安全查詢安全腳本等核心功能。 摘要:?螞蟻區塊鏈摩斯安全計算平臺針對數據安全信任、個人隱私保護以及數據基礎設施不足等痛點,秉持數據可用不可見和將計算移動到數據端的原則,借助區塊鏈、密碼學、隱私保護、安全多方計算、可信計算等前沿...
摘要:預覽地址項目地址初衷在前端業務上生產的時候,可能仍然有部分功能需要被隱藏,只有達成特定的條件才能夠顯示,這些功能可以被稱作為實驗室功能。參考了上述多種做法,提出了使用摩斯電碼節奏作為手勢密碼,開啟實驗室功能的想法。 showImg(https://segmentfault.com/img/bVYHYF?w=922&h=271); 預覽地址:https://jrainlau.github...
閱讀 1794·2021-11-18 10:02
閱讀 3524·2021-11-16 11:45
閱讀 1786·2021-09-10 10:51
閱讀 2106·2019-08-30 15:43
閱讀 1372·2019-08-30 11:23
閱讀 1484·2019-08-29 11:07
閱讀 1892·2019-08-23 17:05
閱讀 1394·2019-08-23 16:14