error: C++ requires a type specifier for all declarations

力扣1189. Maximum Number of Balloons中,遇到一个编译错误:

Line 4: Char 5: error: C++ requires a type specifier for all declarations
rep[‘a’]=rep[‘b’]=rep[‘n’]=1;

源代码片如下:

class Solution {
    
    
    unordered_map<char,int> rep,cnt;
public:
    rep['a']=rep['b']=rep['n']=1;
    rep['l']=rep['o']=2;
    int maxNumberOfBalloons(string text) {
    
    
        cnt['a']=cnt['b']=cnt['n']=cnt['l']=cnt['o']=0;
        for(int i=0;i<text.size();i++){
    
    
            if(rep.find(text[i])!=rep.end()){
    
    
                cnt[text[i]]++;
            }
        }
        int ans=11000;
        for(unordered_map<char,int>::iterator it=cnt.begin();it!=cnt.end();it++){
    
    
            ans=min(ans,it->second/rep[it->first]);
        }
        return ans;
    }
};

提示第四行出错

错误原因

将需要执行的语句写在了函数外面;

解决办法

将赋值语句写在int maxNumberOfBalloons(string text)函数体内。

猜你喜欢

转载自blog.csdn.net/weixin_44321570/article/details/112854359