题目

国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: "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的更多相关文章

  1. Leetcode804.Unique Morse Code Words唯一摩尔斯密码词

    国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: "a" 对应 ".-", "b" 对应 &q ...

  2. Unique Morse Code Words

    Algorithm [leetcode]Unique Morse Code Words https://leetcode.com/problems/unique-morse-code-words/ 1 ...

  3. 【Leetcode】804. Unique Morse Code Words

    Unique Morse Code Words Description International Morse Code defines a standard encoding where each ...

  4. 【Leetcode_easy】804. Unique Morse Code Words

    problem 804. Unique Morse Code Words solution1: class Solution { public: int uniqueMorseRepresentati ...

  5. 804. Unique Morse Code Words - LeetCode

    Question 804. Unique Morse Code Words [".-","-...","-.-.","-..&qu ...

  6. [Swift]LeetCode804. 唯一摩尔斯密码词 | Unique Morse Code Words

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

  7. Leetcode 804. Unique Morse Code Words 莫尔斯电码重复问题

    参考:https://blog.csdn.net/yuweiming70/article/details/79684433 题目描述: International Morse Code defines ...

  8. [LeetCode] Unique Morse Code Words 独特的摩斯码单词

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

  9. (string 数组) leetcode 804. Unique Morse Code Words

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

随机推荐

  1. c++ 和 matlab 下的caffe模型输入差异

    在向一个caffe模型传递输入数据的时候,要注意以下两点: 1. opencv中Mat数据在内存中的存放方式是按行存储,matlab中图像在内存中的存放方式是按列存储. 2. opencv中Mat数据 ...

  2. Keepalived & Lvs集群搭建实验

    实验拓扑图: 实验原理: Keepalived 是基于 LVS ,并与 LVS 高度融合的 LVS和keepalived的关系:lvs起的是负载均衡功能,而keepalived则是高可用(热 备)的支 ...

  3. 《nginx 三》实现nginx的动态负载均衡——实战

    Http动态负载均衡 什么是动态负载均衡 传统的负载均衡,如果Upstream参数发生变化,每次都需要重新加载nginx.conf文件, 因此扩展性不是很高,所以我们可以采用动态负载均衡,实现Upst ...

  4. asp.net webForm也可以这样用Ajax -- My Ajax Framework [全屏看文]

    对于asp.net WebForm项目,进行Ajax操作大概有三种方式:web服务(.asmx文件)  ,  一般处理程序(.ashx)和  一些Ajax控件. 对于.net提供的ajax控件,暂且不 ...

  5. Win2D 官方文章系列翻译 - 与 Direct2D 互操作

    本文为个人博客备份文章,原文地址: http://validvoid.net/win2d-interop-with-direct2d/ Win2D 作为 Direct2D 的上层实现,支持与其进行双向 ...

  6. HihoCoder#1509 : 异或排序(二进制)

    题意 题目链接 Sol 挺简单的吧.考虑两个元素什么时候不满足条件 设\(a_i\)与\(a_i + 1\)最高的不同位分别为0 1,显然\(S\)的这一位必须为\(0\),否则这一位必须为\(1\) ...

  7. CSS3伪类使用方法实例

    有时候在网页设计中会涉及到很多看上去很繁琐的设计图,这时候都会很让人头疼,那么这时候伪类就可以来帮助你解决问题了. 下面有一段实例大家可以参考一下: <!DOCTYPE html> < ...

  8. 云集微助手安装教程和授权说明old

    安装教程 一 .手机越狱(如果已经越狱请跳过此步,直接进行第二步) 越狱教程:http://jailbreak.25pp.com/yueyu/ 二 .安装触动精灵(如果你已安装触动精灵最新版请跳过此步 ...

  9. Android无需权限显示悬浮窗

    TYPE_TOAST一直都可以显示, 但是用TYPE_TOAST显示出来的在2.3上无法接收点击事件, 因此还是无法随意使用. 下面是我之前研究后台线程显示对话框的时候记得笔记, 大家可以看看我们项目 ...

  10. Razor 语法糖常规用法

    1.隐匿代码表达式 例: @model.name 会将表达式的值计算并写入到响应中,输入时采用html编码方式 2.显示表达式 例:@(model.name)会将输入@model.name字符串 3. ...