LeetCode刷题: 【1160】 拼写单词(c++语法相关:map遍历、for标签)

1. 题目

LeetCode刷题: 【1160】 拼写单词(c++语法相关:map遍历、for标签)_第1张图片
LeetCode刷题: 【1160】 拼写单词(c++语法相关:map遍历、for标签)_第2张图片

2. 解题思路

计数即可

3. 代码

class Solution {
public:
    int countCharacters(vector<string>& words, string chars) {
        unordered_map<char, int> chars_map;
        for(char s : chars){
            chars_map[s]++; 
        }

        int ans = 0;
        see:for(string word : words){
            unordered_map<char, int> chars_word_map;
            for(char s : word){
                chars_word_map[s]++; 
            }
            int add = word.size();
            for(auto wc : chars_word_map){
                if(wc.second > chars_map[wc.first]){
                    add = 0;
                }
            }
            ans += add;
        }

        return ans;
    }
};

4. c++ map的遍历方式

for(map<string,string>::iterator it = map.begin(); it!=map.end(); it++)     
        cout<<it->first<<"\t";    
        cout<<it->second<<endl;    
        it++;    
}    

5. c++中没有像java一样的break标签用法

你可能感兴趣的:(#,LeetCode,#,C/C++)