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 ...
随机推荐
- bzoj2049 线段树 + 可撤销并查集
https://www.lydsy.com/JudgeOnline/problem.php?id=2049 线段树真神奇 题意:给出一波操作,拆边加边以及询问两点是否联通. 听说常规方法是在线LCT, ...
- Hbase学习03
第3章 Hbase数据存储模型与工作组件 Data格式设计的的总体原则是按照需求要求,依据Hbase性能的相关标准规范和文件,并遵循“统一规范.统一数据模型.统一规划集群.分步实施”的原则,注重实际应 ...
- js验证登录注册
js验证登录注册的优势,在前台直接验证,不需要在后台读取返回数据验证,减轻服务器压力. 登陆验证得必要性,拦截恶意脚本的登录注册攻击.哈哈,当然有些高手是可以直接跳过js验证的. 所以还是后台验证,并 ...
- SpringSecurity3Demo【原】
oschina git地址: https://gitee.com/KingBoBo/SpringSecurity3Demo.git
- golang os包使用笔记
zhangsan os.Stidn 标准输入 os.Stdout 标准输出 os.Stderr 标准错误输出
- TCP简介(一)
1. TCP如何利用IP达到自己目的 1.1 IP特点 无连接 不可靠 1.2 TCP将应用程序的传输数据分割成合适的数据块 1.3 定时器 1.4 延迟确认 1.5 检验和 1.6 流量控制 2. ...
- java 写一个 map reduce 矩阵相乘的案例
1.写一个工具类用来生成 map reduce 实验 所需 input 文件 下面两个是原始文件 matrix1.txt 1 2 -2 0 3 3 4 -3 -2 0 2 3 5 3 -1 2 -4 ...
- Spring boot中使用Mongodb
安装 使用Idea新建Spring boot工程时需要选择Mongodb 或者在工程中加入依赖 Maven: <dependency> <groupId>org.springf ...
- SpringBoot系列: Java应用程序传参和SpringBoot参数文件
===========================向java 程序传参的几种形式:===========================1. 使用 OS 环境变量. 这个不推荐. 2. 使用JVM ...
- [译]Managing Vue.js State with Vuex
原文 准备 安装 Vuex, 是Vue官方出的package, 它不是Vue内置的.需要另外安装. npm install vuex --save 然后,需要在应用启动文件启用Vuex. main.j ...