码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 185周周赛 字节专场

时间:2020-04-19 15:00:45      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:lse   tle   letters   c++   isp   ret   push   格式   orm   

5388. 重新格式化字符串

string reformat(string s) {
    queue<char> nums, letters;
    for(char c:s) {
        if(‘0‘ <= c && c <= ‘9‘) nums.push(c);
        else if(‘a‘ <= c && c <= ‘z‘) letters.push(c);
    }
    int len1 = nums.size(), len2 = letters.size();
    if(abs(len1-len2) > 1) return "";
    string ans = "";
    if(len1 == 0 || len2 == 0) {
        if(len1 == 1 || len2 == 1) {
            ans += len1 == 1 ? nums.front() : letters.front();
            return ans;
        }
        return "";
    }

    if(len1 >= len2) {
        while(!letters.empty()) {
            ans += nums.front();
            nums.pop();
            ans += letters.front();
            letters.pop();
        }
        if(len1 > len2) ans += nums.front();
    }
    else {
        while(!nums.empty()) {
            ans += letters.front();
            letters.pop();
            ans += nums.front();
            nums.pop();
        }
        ans += letters.front();
    }
    return ans;
}

1 5389.点菜展示表

vector<vector<string>> displayTable(vector<vector<string>>& orders) {
    set<string> foodList;
    map<int, map<string,int> > tableList;
    for(auto& o:orders) {
        foodList.insert(o[2]);
        tableList[stoi(o[1])][o[2]]++;
    }
    vector<string> title;
    title.push_back("titile");
    for(auto& f:foodList) {
        title.push_back(f);
    }
    vector<vector<string> > ans;
    ans.push_back(title);
    for(auto& t:tableList) {
        title.push_back(to_string(t.first));
        for(auto& f:foodList) {
            title.push_back(to_string(t.second[f]));
        }
        ans.push_back(title);
    }
    return ans;
}

leetcode 185周周赛 字节专场

标签:lse   tle   letters   c++   isp   ret   push   格式   orm   

原文地址:https://www.cnblogs.com/huangming-zzz/p/12730964.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!