快手20200412招聘

一共有四题,做出了前三题。记录下后面两题,第一幅图是第三题、第二幅图是第四题。

第三题的代码如下,我将第三题简化为了排序问题:

vector<int> WaitInLine(vector<int>& a, vector<int>& b) {
    // write code here
    vector<int>c; c.clear();
    vector<int>out; out.clear();
    for (int i = 0; i < a.size(); i++)
    {
        c.push_back(a[i] - b[i]);
        out.push_back(i + 1);
    }
    //由大到小//
    int num = c.size();
    for(int i=0;i<num-1;i++)
        for (int j = 0; j < num - 1 - i; j++)
        {
            if (c[j] < c[j + 1])
            {
                int temp = c[j + 1];
                c[j + 1] = c[j];
                c[j] = temp;

                int temp2 = out[j + 1];
                out[j + 1] = out[j];
                out[j] = temp2;

            }
        }
    return out;
}

第四题没做出来,但在牛客网上看到了思路,“每次选择周围最少的点进行统计,并将周围的点变为'*' ”,也有人说用状态压缩,但我是小白,没听说过。

int GetMaxStaffs(vector<vector<char> >& pos) {
    // write code here
    //遍历//
    int rows = pos.size();
    int cols = pos[0].size();
    //周围最少点的个数//
    
    int row = 0; int col = 0;
    do
    {
        bool isupdate = false;
        int num = 4;
        for (int i = 0; i < rows; i++)
            for (int j = 1; j < cols; j++)
            {
                int tempNum = 0;
                if (pos[i][j] == '.') {
                    if (i - 1 >= 0 && pos[i - 1][j] == '.')
                    {
                        tempNum++;
                    }
                    if (i + 1 < rows&&pos[i + 1][j] == '.')
                    {
                        tempNum++;
                    }
                    if (j - 1 >= 0 && pos[i][j - 1] == '.')
                    {
                        tempNum++;
                    }
                    if (j + 1 < cols&&pos[i][j + 1] == '.')
                    {
                        tempNum++;
                    }
                }
                if (tempNum>0&&tempNum < num)
                {
                    num = tempNum;
                    row = i;
                    col = j;
                    isupdate = true;
                }
            }
        if (row - 1 >= 0 && pos[row - 1][col] == '.')
        {
            pos[row - 1][col]='*';
        }
        if (row + 1 < rows&&pos[row + 1][col] == '.')
        {
            pos[row + 1][col] = '*';
        }
        if (col - 1 >= 0 && pos[row][col - 1] == '.')
        {
            pos[row][col - 1] = '*';
        }
        if (col + 1 < cols&&pos[row][col + 1] == '.')
        {
            pos[row][col + 1] = '*';
        }
        if (!isupdate)
        {
            break;
        }
    } while (true);

    int out = 0;
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
        {
            if (pos[i][j] == '.')
            {
                out++;
            }
        }
    return out;

}

如果有更好的方法,欢迎留言哈。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CHPCWWHSU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值