资讯专栏INFORMATION COLUMN

2.leetcode唯一的摩斯密码

FreeZinG / 3178人阅读

摘要:题目自己的解决方法其他解决方法

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/102977.html

相关文章

  • 2.leetcode唯一摩斯密码

    摘要:题目自己的解决方法其他解决方法 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...

    XanaHopper 评论0 收藏0
  • 中文摩斯密码 - JavaScript库

    摘要:中文摩斯密码是一个使用编写的用于浏览器和环境的摩斯密码库,可以支持,即支持中文的摩斯密码解密和加密。安装引入使用只有两个,名字为对于例子如下越过长城,走向世界对于例子如下相关地址在线开源地址 中文摩斯密码 - Xmorse Xmorse 是一个使用 Javascript 编写的用于浏览器和 nodejs 环境的摩斯密码库,可以支持 unicode,即支持中文的摩斯密码解密和加密。 1...

    MadPecker 评论0 收藏0
  • 数据可用不可见!揭秘蚂蚁区块链摩斯安全计算平台

    摘要:安全透明轻量的蚂蚁区块链摩斯安全计算基础设施蚂蚁摩斯依托蚂蚁金融科技平台,结合区块链技术,将复杂的隐私保护与密码学算法透明化产品化,提供安全发布安全模型安全统计安全查询安全脚本等核心功能。 摘要: 蚂蚁区块链摩斯安全计算平台针对数据安全信任、个人隐私保护以及数据基础设施不足等痛点,秉持数据可用不可见和将计算移动到数据端的原则,借助区块链、密码学、隐私保护、安全多方计算、可信计算等前沿...

    zhisheng 评论0 收藏0
  • 花样招聘面试题

    摘要:由于我们得到摩斯密码没有空格隔开,所以解密后有可能不止一种。完整的代码得到结果如下,根据图片中的提示,该单词与面试有关,那么应该是无疑。上面的代码我们用了层嵌套循环,确实有点多,但是只有条件成立,才会进入深层的循环。 残缺的地图 今天在微信群里面看到一张招聘图片,如下 showImg(https://segmentfault.com/img/bVbhvww?w=600&h=600); ...

    e10101 评论0 收藏0
  • RhykeJS——专为开启“实验室功能”手势密码

    摘要:预览地址项目地址初衷在前端业务上生产的时候,可能仍然有部分功能需要被隐藏,只有达成特定的条件才能够显示,这些功能可以被称作为实验室功能。参考了上述多种做法,提出了使用摩斯电码节奏作为手势密码,开启实验室功能的想法。 showImg(https://segmentfault.com/img/bVYHYF?w=922&h=271); 预览地址:https://jrainlau.github...

    用户83 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<