Unique Morse Code Words
Algorithm
【leetcode】Unique Morse Code Words
https://leetcode.com/problems/unique-morse-code-words/
1)problem
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 "--...--.".
Note:
The length of words will be at most 100.
Each words[i] will have length in range [1, 12].
words[i] will only consist of lowercase letters.
2)answer
将26个因为字母映射为摩斯电码,然后根据每组字母每个字符对应的摩斯电码组合起来。至于那个简写是为什么可以那么写,没搞清楚。【"cba" can be written as "-.-..--...", (which is the concatenation "-.-." + "-..." + ".-").】
3)solution
#include "pch.h"
#include <stdio.h>
#include <string>
#include <vector>
#include <unordered_set>
using std::string;
using std::vector;
using std::unordered_set;
class Solution {
public:
int uniqueMorseRepresentations(vector<string>& words) {
vector<string> morse_code = { ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.." };
// vector<string> store;
unordered_set<string> result_val;
int count = 0;
for (auto str: words)
{
string tmp;
for (auto ch: str)
{
//-'a'是找到输入字的索引。例如,'a' - 'a'为0.所以'a'对应于morse_code中的第一个元素。
tmp += morse_code[(int)ch - (int)('a')];
}
result_val.insert(tmp);
}
return result_val.size();
}
};
int main()
{
vector<string> words;
words.push_back("cba");
// 使用内容
Solution nSolution;
nSolution.uniqueMorseRepresentations(words);
}
Unique Morse Code Words的更多相关文章
- 【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 ...
- Leetcode 804. Unique Morse Code Words 莫尔斯电码重复问题
参考:https://blog.csdn.net/yuweiming70/article/details/79684433 题目描述: International Morse Code defines ...
- [Swift]LeetCode804. 唯一摩尔斯密码词 | Unique Morse Code Words
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- [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 ...
- 804. Unique Morse Code Words
Description International Morse Code defines a standard encoding where each letter is mapped to a se ...
- LeetCode - 804. Unique Morse Code Words
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
随机推荐
- M1-Flask-Day2
内容概要: 1.flask - 蓝图 - 中间件 - 闪现 2.扩展 - session - wtfrom 3.上下文管理 - local-threading 4.websocket - 轮训 - 长 ...
- PV、UV、UIP、VV、CPC、CPM、RPM、CTR解释
PV.UV.UIP.VV.CPC.CPM.RPM.CTR 具体解释 PV:Page View,页面访问量,也就是曝光量. UV:Unique Visitor,独立访客数,同一个访问多次访问也只算1个访 ...
- centos7下安装pip以及mysql等软件
1.安装pip 安装失败了的提示: No package pip available.Error: Nothing to do 解决方法: 需要先安装扩展源EPEL. EPEL(http://fedo ...
- docker 基础之数据管理
数据卷 一.将本地默认目录挂载到docker容器内指定的目录 #将本地的目录挂在到docker容器内 docker run -it --name container-test -h CONTAINER ...
- iptables之端口限速
#iptables -A FORWARD -p tcp -m tcp --sport 10000 -m limit --limit 500/sec --limit-burst 1000 -j ACCE ...
- 【SSL】WebClient 请求 https 页面出错:未能创建 SSL/TLS 安全通道
#问题: 当向一个https的url上发送请求,报错:未能创建 SSL/TLS 安全通道: using (WebClient client = new WebClient()) { string ad ...
- 1、JPA-HelloWorld
/** * JPA 是 hibernate 的一个抽象(就像JDBC和JDBC驱动的关系) * JPA 本质上是一种 ORM 规范,不是 ORM 框架,因为 JPA 并未提供 ORM 实现,只是制订了 ...
- Spark Submitting Applications浅析
Submitting Applications提交应用程序 在spark的bin目录下spark-submit脚本被用于在集群中启动应用程序.它可以通过一个统一的接口来使用Spark支持的所有集群管理 ...
- js 图片压缩上传(纯js的质量压缩,非长宽压缩)
下面是大神整理的demo,很实用,这里存一下备用,感谢大神! 此demo为大于1M对图片进行压缩上传 若小于1M则原图上传,可以根据自己实际需求更改. demo源码如下 <!DOCTYPE ht ...
- Newtonsoft.Json添加项
JObject jo = (JObject)JsonConvert.DeserializeObject(result); ") { string domain=(jo["data& ...