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;
}

图片说明
图片说明

全部评论

相关推荐

09-17 19:25
已编辑
太原理工大学 游戏测试
叁六玖:公司名发我,我要这个HR带我打瓦
我的秋招日记
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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