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的更多相关文章

  1. 【Leetcode】804. Unique Morse Code Words

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

  2. 【Leetcode_easy】804. Unique Morse Code Words

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

  3. 804. Unique Morse Code Words - LeetCode

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

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

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

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

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

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

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

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

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

  8. 804. Unique Morse Code Words

    Description International Morse Code defines a standard encoding where each letter is mapped to a se ...

  9. LeetCode - 804. Unique Morse Code Words

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

随机推荐

  1. 10款Mac上程序员装机必备的开发工具推荐和下载

    10款Mac上程序员装机必备的开发工具推荐和下载 使用Mac的用户主要有两大类:设计师和程序员,为各位程序员童鞋推荐10个Mac上非常棒的开发工具和辅助工具,分享软件专题[10款Mac上程序员装机必备 ...

  2. nginx中间件

    Nginx简介 Nginx是一个开源且高性能.可靠的HTTP中间件.代理服务.其特点是占有内存少,并发能力强. Nginx优势:IO多路复用epoll 1.什么是IO复用 它是内核提供的一种同时监控多 ...

  3. Python中字符串的操作

    字符串的基本详情 用单引号或者双引号包含的内容 不支持直接在内存中修改 可支持索引.切片.成员检查.长度查看 字符串赋值到变量 str1 = 'hello world' 字符串打印查看 str1 = ...

  4. Ubuntu 下使用 putty并通过 ch340 usb 串口线进行调试

    安装putty sudo apt-get install putty -y 插入usb转串口线 由于linux下没有Windos类似的设备管理器,所以我们可以通过其他方法获取对应的串口号 可以在插拔之 ...

  5. WebStorm记录(3)

    连通接口 接口 我自己理解前后端传输数据都是通过ajax方式 一般使用get和post两种方式传输数据 GET 接口 接口描述 获取登录验证码图片及密钥 HTTP方法 POST URL /captch ...

  6. jQuery使用(四):DOM操作之查找兄弟元素和父级元素

    查找兄弟元素 向下查找兄弟元素 next() nextAll() nextUntil() 向上查找兄弟元素 prev() prevAll() prevUntil() 查找所有兄弟元素 siblings ...

  7. HDU - 1255 覆盖的面积 (线段树求面积交)

    https://cn.vjudge.net/problem/HDU-1255 题意 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. 分析 求面积并的题:https://www.cnbl ...

  8. 068、Calico的网络结构是什么?(2019-04-11 周四)

    参考https://www.cnblogs.com/CloudMan6/p/7520164.html     root@host1:~# docker run -itd --name bbox1 -- ...

  9. 042、用volume container 共享数据 (2019-03-05 周二)

    参考https://www.cnblogs.com/CloudMan6/p/7188479.html   volume container 是专门为其他容器提供 volume 的容器,他提供的卷也可以 ...

  10. 细说JDK日志组件

    1. 概述 JDK自带的日志组件在包java.util.logging下,如图: 2. 架构如上图所示,JDK日志组件核心元素包括:Logger,Handler,Filter和Formatter,他们 ...