zoukankan      html  css  js  c++  java
  • 804. Unique Morse Code Words

    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.
    

    思路

    Solution (C++):

    int uniqueMorseRepresentations(vector<string>& words) {
        vector<string> morse_code{".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        unordered_set<string> s;
        for (auto w : words) {
            string tmp = "";
            for (auto ch : w) {
                tmp += morse_code[ch-'a'];
            }
            s.insert(tmp);
        }
        return s.size();
    }
    

    性能

    Runtime: 4 ms  Memory Usage: 7 MB

    思路

    Solution (C++):

    
    

    性能

    Runtime: ms  Memory Usage: MB

    相关链接如下:

    知乎:littledy

    欢迎关注个人微信公众号:小邓杂谈,扫描下方二维码即可

    作者:littledy
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    目标检测之YOLOv3
    残差网络(ResNet)
    FPN详解
    YOLOv2/YOLO 9000深入理解
    批归一化(BN)
    全卷积网络FCN
    基于深度学习的目标检测算法综述
    目标检测两个基础部分——backbone and detection head
    YOLOv1 深入理解
    内置模块
  • 原文地址:https://www.cnblogs.com/dysjtu1995/p/12738304.html
Copyright © 2011-2022 走看看