iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 5
0
自我挑戰組

資料結構大便當系列 第 5

[Day 5] Sets 集合

  • 分享至 

  • xImage
  •  

怕最後不夠寫 30天,連 set 也拿來用一下


Sets 集合,即一堆東西,該堆東西可能具有某種特定性質的事物,並將數個物件歸類而分成為一個或數個形態各異的大小整體

而在 C++ 中,set 主要操作有:

  1. insert: 加入 element
  2. erase: 移除 element
  3. swap: 交換一堆東西
  4. find: 找出 element
  5. count: 計算 element

今天一樣以一題 leetcode 結束這一回合:

  1. 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, "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.
int uniqueMorseRepresentations(vector<string>& words) {
    vector<string> morse_code = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
    unordered_set<string> gen_codes;
    
    for(auto word : words) {
        string code = "";
        for(auto ch : word)
            code += morse_code[ch - 'a'];
        gen_codes.insert(code);
    }
    
    return gen_codes.size();
}

上一篇
[Day 4] Hashtable,練習做一張表格
下一篇
[Day 6] pair 數對
系列文
資料結構大便當30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言