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

1. 题目

在这里插入图片描述
在这里插入图片描述

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标签用法

猜你喜欢

转载自blog.csdn.net/Activity_Time/article/details/104932040