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 ...
随机推荐
- ColorCtr控制颜色渐变
ColorCtr控制颜色渐变 public class ColorCtr : MonoBehaviour { public static global_color Instance; ...
- 如何解决 “the specified service is marked as deletion”
在部署windows service应用程序,突然发生了如下问题:“the specified service is marked as deletion”.导致windows service不能部署 ...
- jQuery Validate验证框架详解(转)
jQuery校验官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation 一.导入js库 <script type=& ...
- output引用类型
1.创建存储过程,将参数avg设置为引用参数,可以对参数有反馈作用 ), @avg float output) as select @avg=AVG(price) from goods where g ...
- Chrome浏览器安装vue-devtools插件
插件功能:方便在浏览器调试vue代码 插件git地址:https://github.com/vuejs/vue-devtools 因为chrome要FQ,打不开,所以不能直接进去安装拓展程序,只能选择 ...
- Eclipse常用设置和快捷键
1.提示键配置一般默认情况下,Eclipse ,MyEclipse 的代码提示功能是比Microsoft Visual Studio的差很多的,主要是Eclipse ,MyEclipse本身有很多选项 ...
- 将零散文件使用ICSharpCode.SharpZipLib压缩打包后一次性下载
public static Stream CreateZip(List<string> listPath, int level = 5) { MemoryStream mstream = ...
- time和datetime模块
在Python中,通常有这几种方式来表示时间: 1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素. 由于Python的time模块实现主要调用C库,所以各个平台可能有 ...
- tomcat下部署项目的流程和遇到的问题笔记
简单部署流程: 1,解析域名关联到服务器ip 2,配置服务器jre运行环境 3,安装tomcat 4,项目打war包,放入tomcat根目录下webapps(tomcat默认加载的项目目录)目录下 5 ...
- JAVA StringBuffer的用法
在使用StringBuffer 的时候,习惯性的像String一样把他初始化了 StringBuffer result = null; 结果警告:Null pointer access: The va ...