华为-2022-08-02-ai搜索岗一面二面

https://leetcode.cn/problems/most-common-word/

https://leetcode.cn/problems/the-number-of-the-smallest-unoccupied-chair/

原题

#include 
#include 
#include 
#include 

using namespace std;

int main()
{
    string paragraph;
    unordered_map wordCount;
    unordered_set banned;
    banned.insert("hit");
    // cin >> paragraph;
    paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.";

    int n = paragraph.length();
    int maxCount = 0;
    string maxCountString;

    for (int i = 0; i < n;)
    {
        int j = i;
        string word = "";
        while (j < n && isalpha(paragraph[j]))
        {
            // char c = (paragraph[j]>='A' && paragraph[j]<='Z') ? paragraph[j]+32:paragraph[j];
            // cout<<paragraph[j]<<", "<<c<<"\n";
            word.append(string(1,tolower(paragraph[j])));
            j++;
        }

        wordCount[word]++;

        while (j < n && !isalpha(paragraph[j]))
            j++;
        i = j;
    }

    for (auto &[word, cnt] : wordCount)
    {
        if (cnt > maxCount&&banned.count(word)==0)
        {
            maxCount = cnt;
            maxCountString = word;
        }
    }

    cout<<maxCountString<<"\n";

    return 0;
}
#华为面试##面试##秋招#
全部评论

相关推荐

06-23 11:43
门头沟学院 Java
allin校招的烤冷...:我靠,今天中午我也是这个hr隔一个星期发消息给我。问的问题还是一模一样的😅
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
4
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务