LeetCode804. Unique Morse Code Words
题目
国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: "a" 对应 ".-", "b" 对应 "-...", "c" 对应 "-.-.", 等等。
为了方便,所有26个英文字母对应摩尔斯密码表如下:
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
给定一个单词列表,每个单词可以写成每个字母对应摩尔斯密码的组合。例如,"cab" 可以写成 "-.-..--...",(即 "-.-." + "-..." + ".-"字符串的结合)。我们将这样一个连接过程称作单词翻译。
返回我们可以获得所有词不同单词翻译的数量。
例如:
输入: words = ["gin", "zen", "gig", "msg"]
输出: 2
解释:
各单词翻译如下:
"gin" -> "--...-."
"zen" -> "--...-."
"gig" -> "--...--."
"msg" -> "--...--."
共有 2 种不同翻译, "--...-." 和 "--...--.".
注意:
- 单词列表
words的长度不会超过100。 - 每个单词
words[i]的长度范围为[1, 12]。 - 每个单词
words[i]只包含小写字母。
考点
1.无序关联容器set的唯一性,关键字等于值。
2. set.insert(key);
思路
无序关联容器set不重复,关键字类型string。循环:顺序容器里的每个单词。再循环:求出每一个单词的摩斯码,t+=morse[c-'a'],翻译字符,插入关联容器中,可以去除重复。
代码
class Solution {
public:
int uniqueMorseRepresentations(vector<string>& words) {
vector<string> morse{".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
unordered_set<string> s;
for(string word:words)
{
string t="";
for(char c:word)
{
t+=morse[c-'a'];
}
s.insert(t);
}
return s.size();
}
};
问题
LeetCode804. Unique Morse Code Words的更多相关文章
- Leetcode804.Unique Morse Code Words唯一摩尔斯密码词
国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: "a" 对应 ".-", "b" 对应 &q ...
- Unique Morse Code Words
Algorithm [leetcode]Unique Morse Code Words https://leetcode.com/problems/unique-morse-code-words/ 1 ...
- 【Leetcode】804. Unique Morse Code Words
Unique Morse Code Words Description International Morse Code defines a standard encoding where each ...
- 【Leetcode_easy】804. Unique Morse Code Words
problem 804. Unique Morse Code Words solution1: class Solution { public: int uniqueMorseRepresentati ...
- 804. Unique Morse Code Words - LeetCode
Question 804. Unique Morse Code Words [".-","-...","-.-.","-..&qu ...
- [Swift]LeetCode804. 唯一摩尔斯密码词 | 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 ...
- [LeetCode] Unique Morse Code Words 独特的摩斯码单词
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- (string 数组) leetcode 804. Unique Morse Code Words
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
随机推荐
- [原创]Dubbo配置(Spring4+Hiberante4+Druid)
如果dubbo使用注解,并且spring也使用注解,如使用事务,则dubbo加过注解的类无法发布. <?xml version="1.0" encoding="UT ...
- PHP中的header()函数
PHP header 函数的用法及其注意事项 void header ( string $string [, bool $replace = true [, int $http_response_co ...
- Python 参数设置
1. 配置文件(ConfigParser模块) 1.1 ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.s ...
- ubuntu下mysql安装(server、client、dev),开启、停止和重启,及常见错误
转自:ubuntu下mysql安装(server.client.dev),开启.停止和重启,及常见错误 1. 在ubuntu下安装server和client很简单: (1)安装server apt-g ...
- Day4上午
expect100+50+50, In fact 100+10+0. 代码能力还有待提高,部分分应该能拿的.结果...力不从心啊. T1 贪心做的不知对不对. 看来思路是对的,不知道能不能对. 暴力做 ...
- java高级技术交流群
<明天的地平线>专注Java相关技术:SpringBoot.SpringCloud.MyBatis.Docker.微服务.集群.分布式.Linux.Jenkins.Netty.Angula ...
- 【转载】了解CSS/CSS3原生变量var
文章转载自:鑫空间鑫生活(https://www.zhangxinxu.com/) 原文链接:http://www.zhangxinxu.com/wordpress/?p=5804 内容摘要: 在任何 ...
- 登录mysql数据库出现 : ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO) ER或者忘记密码
1. 在安装mysql的文件目录中找到配置文件my.ini ,然后右击用记事本打开 2. 打开后,搜索mysqld关键字 找到后,在mysqld下面添加skip-grant-tabl ...
- 在 Excel 中设置图片
package com.smbea.demo.excel; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStr ...
- Mautic-2.2.0 (Ubuntu 16.04)
平台: Ubuntu 类型: 虚拟机镜像 软件包: mautic-2.2.0 business intelligence commercial ecommerce mautic open-source ...