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)函数体内。

  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值