leetcode10002

https://leetcode.com/problems/find-common-characters/

class Solution {
public:
    vector<string> commonChars(vector<string>& A) {
        // set<char> s;
        int cnt1[26]{INT_MAX};
        memset(cnt1,1,sizeof(cnt1));
        // fill(cnt1,cnt1+26,INT_MAX);
        for(int i = 0 ; i< 26;i++) cout<<cnt1[i]<<endl;
        vector<int> cnt(26,INT_MAX);
        for(auto a:A){
            int c[26]{0};
            for(auto ss:a) c[ss-'a']++;
             for(int i = 0 ; i < 26 ;i++) cnt[i] = min(c[i],cnt[i]);
        }
        // cout<<cnt[2] <<endl;
        vector<string> res;
        int n;
        for(int i = 0;i<26;i++){
            if(cnt[i]!= 0 ){
                n = cnt[i];
                while(n--){
                    res.emplace_back(string(1, i + 'a'));
                }
            }
        }
        return res;
    }
};

两个小收获:
1.对于int数据的赋值,不能使用int a[26]{INT_MAX}来。
应当使用fill(a,a+26,INT_MAX);
2.从char变成string,可以使用string(1,‘a’+i);
第一个参数表示后面的char重复的次数;后面’a’+i表示a后面第i个字母。
这里相当于构造函数,只是没有名字而已,叫做临时变量。

	string str1(5, 'a');
	cout << str1 << endl;//aaaaa
string NoName(5, 'a');
string (5, 'a');
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值