摘要:题目自己的解决方法其他解决方法
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 };
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/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...
阅读 1718·2021-11-18 10:02
阅读 3496·2021-11-16 11:45
阅读 1747·2021-09-10 10:51
阅读 2041·2019-08-30 15:43
阅读 1345·2019-08-30 11:23
阅读 1445·2019-08-29 11:07
阅读 1860·2019-08-23 17:05
阅读 1314·2019-08-23 16:14