在 Leetcode 上基于摩尔斯电码的“简单”问题中需要的建议

问题描述

问题链接 - https://leetcode.com/problems/unique-morse-code-words/

我正在尝试对 类型的 unordered_map 中的所有 a-z 字符进行映射

在 For 循环中,我将相应的值附加到一个字符串中,该字符串在“words”的每个“word”之后初始化

但我不断收到错误 -

Line 41: Char 33: error: no viable overloaded operator[] for type 'unordered_map<std::string,std::string>' (aka 'unordered_map<basic_string<char>,basic_string<char>>')
                str = str + (ump[words[i][j]]);
                             ~~~^~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/unordered_map.h:984:7: note: candidate function not viable: no kNown conversion from '__gnu_cxx::__alloc_traits<std::allocator<char>,char>::value_type' (aka 'char') to 'const std::unordered_map<std::__cxx11::basic_string<char>,std::__cxx11::basic_string<char>,std::hash<std::string>,std::equal_to<std::__cxx11::basic_string<char>>,std::allocator<std::pair<const std::__cxx11::basic_string<char>,std::__cxx11::basic_string<char>>>>::key_type' (aka 'const std::__cxx11::basic_string<char>') for 1st argument
      operator[](const key_type& __k)
      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/unordered_map.h:988:7: note: candidate function not viable: no kNown conversion from '__gnu_cxx::__alloc_traits<std::allocator<char>,char>::value_type' (aka 'char') to 'std::unordered_map<std::__cxx11::basic_string<char>,std::__cxx11::basic_string<char>>>>::key_type' (aka 'std::__cxx11::basic_string<char>') for 1st argument
      operator[](key_type&& __k)
      ^

我的代码 -

     class Solution {
     public:
     int uniqueMorseRepresentations(vector<string>& words) {
        
        unordered_map<string,string> ump;
        ump["a"] = ".-";
        ump["b"] = "-...";
        ump["c"] ="-.-.";
        ump["d"] = "-..";
        ump["e"] = ".";
        ump["f"] = "..-.";
        ump["g"] = "--.";
        ump["h"] = "....";
        ump["i"] = "..";
        ump["j"] = ".---";
        ump["k"] = "-.-";
        ump["l"] = ".-..";
        ump["m"] = "--";
        ump["n"] = "-.";
        ump["o"] = "---";
        ump["p"] = ".--.";
        ump["q"] = "--.-";
        ump["r"] = ".-.";
        ump["s"] = "...";
        ump["t"] = "-";
        ump["u"] = "..-";
        ump["v"] = "...-";
        ump["w"] = ".--";
        ump["x"] = "-..-";
        ump["y"] = "-.--";
        ump["z"] = "--..";
        
        
        set<string> s;
        string str;
        for(int i=0;i<words.size();i++)
        {
            str="";
            for(int j=0;j<words[i].size();i++)
            {
                str = str + (ump[words[i][j]]);
            }
            s.push(str);
        }
        
        return s.size();
    }
};```


解决方法

我没有检查代码的逻辑。从语言的角度来看,我已经纠正了你代码中的错误

 unordered_map<char,string> ump;
    ump['a'] = ".-";
    ump['b'] = "-...";
    ump['c'] ="-.-.";
    ump['d'] = "-..";
    ump['e'] = ".";
    ump['f'] = "..-.";
    ump['g'] = "--.";
    ump['h'] = "....";
    ump['i'] = "..";
    ump['j'] = ".---";
    ump['k'] = "-.-";
    ump['l'] = ".-..";
    ump['m'] = "--";
    ump['n'] = "-.";
    ump['o'] = "---";
    ump['p'] = ".--.";
    ump['q'] = "--.-";
    ump['r'] = ".-.";
    ump['s'] = "...";
    ump['t'] = "-";
    ump['u'] = "..-";
    ump['v'] = "...-";
    ump['w'] = ".--";
    ump['x'] = "-..-";
    ump['y'] = "-.--";
    ump['z'] = "--..";
    
    
    set<string> s;
    string str;
    for(int i=0;i<words.size();i++)
    {
        str="";
        for(int j=0;j<words[i].size();j++)
        {
            str = str + (ump[words[i][j]]);
        }
        s.insert(str);
    }
    
    return s.size();