# leetcode-804-Unique Morse Code Words

### 题目描述：

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, "cab" 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.

### 要完成的函数：

int uniqueMorseRepresentations(vector<string>& words)

### 说明：

1、这道题给定一个vector，里面存放着多个字符串，字符串中的每一个字母都对应一个摩尔斯码。要求把给定的字符串翻译成摩尔斯码，最后返回有多少种不同的摩尔斯码。

2、明白题意，这道题也没有什么快速的方法来实现，就直接暴力解法做吧。

```    int uniqueMorseRepresentations(vector<string>& words)
{
set<string>res;//存储翻译之后的摩尔斯码
vector<string>morse={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
int s1=words.size(),s2;
string t;//临时变量
for(int i=0;i<s1;i++)//逐一取出字符串
{
s2=words[i].size();
t="";
for(int j=0;j<s2;j++)//逐一取出字母
t+=morse[words[i][j]-'a'];//翻译成摩尔斯码
res.insert(t);//存储到set中
}
return res.size();
}```

0 条评论

## 相关文章

### 最佳加法表达式

给定n个1到9的数字，要求在数字之间最多添加m个加号(加号两边必须有数字，并且不能有两个或两个以上加号相邻），使得所得到的加法表达式的值最小，并输出该值。

20020

10820

14930

14200

### go基础编程 day-2

零值并不等于空值，而是当变量声明为某种来兴后的默认零值，通常情况下默认值为0，bool为false，string为空字符串。

11920

39490

32140

386130

### c语言格式大整理

1、C语言中，非零值为真，真用1表示；零值为假，假用0表示。 2、转义字符参考： \a 蜂鸣，响铃  \b 回退：向后退一格 ...

62270

24040