LeetCode 804 Unique Morse Code Words 解题报告
题目要求
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.
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.
题目分析及思路
题目给出摩斯码的编码规则,要求返回所有单词不同transformation的个数。找出多少种不同的情况,可以用len(set())的方式进行处理。摩斯码和字母的对应关系可以用字典。
python代码
class Solution:
def uniqueMorseRepresentations(self, words):
"""
:type words: List[str]
:rtype: int
"""
morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
letter = ['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']
mldict = dict(zip(letter,morse))
res = set()
for word in words:
mword = ""
for w in word:
mword = mword + mldict[w]
res.add(mword)
return len(res)
LeetCode 804 Unique Morse Code Words 解题报告的更多相关文章
- 【LeetCode】804. Unique Morse Code Words 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 set + map set + 字典 日期 题目地 ...
- LeetCode 804. Unique Morse Code Words (唯一摩尔斯密码词)
题目标签:String 题目给了我们 对应每一个 字母的 morse 密码,让我们从words 中 找出 有几个不同的 morse code 组合. 然后只要遍历 words,把每一个word 转换成 ...
- [LeetCode] 804. Unique Morse Code Words 独特的摩斯码单词
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- Leetcode 804. Unique Morse Code Words 莫尔斯电码重复问题
参考:https://blog.csdn.net/yuweiming70/article/details/79684433 题目描述: International Morse Code defines ...
- (string 数组) leetcode 804. Unique Morse Code Words
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- LeetCode - 804. Unique Morse Code Words
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- [LeetCode] 804. Unique Morse Code Words_Easy tag: Hash Table
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- 804. Unique Morse Code Words - LeetCode
Question 804. Unique Morse Code Words [".-","-...","-.-.","-..&qu ...
- 【Leetcode_easy】804. Unique Morse Code Words
problem 804. Unique Morse Code Words solution1: class Solution { public: int uniqueMorseRepresentati ...
随机推荐
- Goldengate:ERROR 180 encountered commit SCN that is not greater than the highest SCN already processed
How to recover from Extract ERROR 180 encountered commit SCN that is not greater than the highest SC ...
- Oracle的NVL函数用法
从两个表达式返回一个非 null 值. 语法 NVL(eExpression1, eExpression2) 参数eExpression1, eExpression2 如果 eExpression1 ...
- 【iCore4 双核心板_FPGA】例程十六:基于双口RAM的ARM+FPGA数据存取实验
实验现象: 核心代码: int main(void) { /* USER CODE BEGIN 1 */ int i; int address,data; ; ]; ]; char *p; /* US ...
- 【iCore1S 双核心板_ARM】例程十五:USB_HID实验——双向数据传输
实验方法: 1.USB_HID协议免驱动,此例程不需要驱. 2.将跳线冒跳至USB_OTG,通过Micro USB 线将iCore1S USB-OTG接口与电脑相连. 3.打开上位机软件usb_hid ...
- android中通过intent传递复杂数据
android中在各个service或者acitivity之间可以通过Intent来传递一些数据,intent原生直接提供了一些简单数据类型的数据的传递,使用起来也很方便,比如int boolean ...
- 教程:SpagoBI开源商业智能之XML Template 图表模板
SpagoBI offers a variety of widgets' examples realized with the Highcharts library, that can be divi ...
- laravel5.4中验证与错误提示设置
1.对于交互提交数据,验证如: #验证 $this->validate(\request(),[ 'title' => 'required|string|min:3|max:20', 'c ...
- MySQL -- 行转列 -- GROUP_CONCAT -- MAX(CASE WHEN THEN)
列转行:利用max(case when then) SELECT `name`, MAX( CASE WHEN course='语文' THEN score END ) AS 语文, MAX( CAS ...
- Fiddler 使用命令行
在 Fiddler 界面左下角处,可以输出一些快捷命令,常用的快捷命令如下: help:查看命令帮助cls:清屏,即清空会话列表中的所有会话select:选择某一类型的会话,如 select html ...
- 【zheng环境准备】安装activemq
1.下载http://activemq.apache.org/activemq-5140-release.html 2.移动到linux机器上 3.解压 tar -zxvf apache-active ...