9.11 小米

第一题 数组移动

过了 0.83
这么解题有一定问题
图片说明

第二题 2048

A 了

string leftMove(string str) {
    vector<int> arr;
    stringstream ss(str);
    int tem;
    while(ss >> tem)
        arr.push_back(tem);
    int m = arr.size();
    stack<int> sta;
    for(int i=0; i<m; i++) {
        if(!sta.empty() && arr[i] == sta.top()) {
            sta.pop();
            sta.push(2*arr[i]);
            if(++i < m && arr[i] != 0) sta.push(arr[i]);
        } else if(arr[i] != 0) sta.push(arr[i]);
    }
    string res = "";
    m -= sta.size();
    while(!sta.empty()) {
        res = to_string(sta.top()) + ' ' + res;
        sta.pop();
    }
    for(; m>0; m--)
        res += "0 ";
    if(res.back() == ' ') res.pop_back();
    return res;
}

string solution(vector < string > input) {
    string res = "";
    for(auto &it:input)
        res += leftMove(it) + "\n";
    return res;
}

图片说明
图片说明

全部评论

相关推荐

在看牛客的社畜很积极:身高体重那一行信息去掉,学校那一行的信息放上面,找半天都没找到你是哪个学校什么专业的
点赞 评论 收藏
分享
夏日狂想曲:连体婴是这样的,不过国内还有上四休三的公司?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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