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

    Description

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

    Analyse

    先通过Morse Code的字典把输入的words变成Morse Code,
    接下来给存放着Morse Code的list去重,得到Morse Code的种类

    1.union + erase去重

    主要是下面这两段代码

    先排序

    unique(morse_codes.begin(), morse_codes.end())这段代码会将相邻的重复的数据挤到末尾并返回一个指向倒数第二个one的迭代器

    one one two three two
    变成
    one two three one two

    接下来使用erase函数删除倒数第二个one到整个vector末尾的元素,去重就完成了

    Removes from the vector either a single element (position) or a range of elements ([first,last)).

    sort(morse_codes.begin(), morse_codes.end());
    morse_codes.erase(unique(morse_codes.begin(), morse_codes.end()), morse_codes.end());
    

    完整代码如下:

    int uniqueMorseRepresentations(vector<string>& words)
    {
        string dic[] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        vector<string> morse_codes;
        for(vector<string>::iterator iter = words.begin(); iter < words.end(); iter++)
        {
            string tmp = "";
            for (int i = 0; i < (*iter).length(); i++)
            {
                int index = (int)((*iter).at(i) - 'a');
                tmp.append(dic[index]);
            }
            morse_codes.push_back(tmp);
        }
    
        sort(morse_codes.begin(), morse_codes.end());
        morse_codes.erase(unique(morse_codes.begin(), morse_codes.end()), morse_codes.end());
        return morse_codes.size();
    }
    

    2.使用unordered_set

    Sets are containers that store unique elements following a specific order.

    Unordered sets are containers that store unique elements in no particular order, and which allow for fast retrieval of individual elements based on their value.

    set 中每个元素的值唯一且有序
    unordered_set 中每个元素的值唯一且无序,这题不需要排序,用这个比较好,但leetcode上set用的时间反而短一些

    int uniqueMorseRepresentations(vector<string>& words)
    {
        string dic[] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        set<string> morse_codes;
    
        for(vector<string>::iterator iter = words.begin(); iter < words.end(); iter++)
        {
            string tmp = "";
            for (int i = 0; i < (*iter).length(); i++)
            {
                int index = (int)((*iter).at(i) - 'a');
                tmp.append(dic[index]);
            }
            morse_codes.insert(tmp);
        }
    
        return morse_codes.size();
    }
    

    Reference

    1. http://www.cplusplus.com/reference/unordered_set/unordered_set/
  • 相关阅读:
    iOS 苹果开发证书失效的解决方案(Failed to locate or generate matching signing assets)
    iOS NSArray数组过滤
    App Store2016年最新审核规则
    iOS 根据字符串数目,自定义Label等控件的高度
    iOS 证书Bug The identity used to sign the executable is no longer valid 解决方案
    Entity FrameWork 增删查改的本质
    EF容器---代理类对象
    Entity FrameWork 延迟加载本质(二)
    Entity FrameWork 延迟加载的本质(一)
    Entity FrameWork 增删查改
  • 原文地址:https://www.cnblogs.com/arcsinw/p/9340603.html
Copyright © 2011-2022 走看看