LEETCODE 506. 相对名次

请添加图片描述

class Solution {
public:
    vector<string> findRelativeRanks(vector<int>& score) {
        vector<string> answer(score.size());
        unordered_map<int, string> scoreRank;
        vector<int> temp(score.size());
        for (int i = 0; i < score.size(); i++) {
            temp[i] = score[i];
        }

        int leftmin, leftmax, rightmin, rightmax;
        for (int step = 1; step < score.size(); step *= 2) {
            for (leftmin = 0; leftmin < (score.size() - step);leftmin=rightmax){
                leftmax = rightmin = leftmin + step;
                rightmax = rightmin + step;
                if (rightmax > score.size() - 1) {
                    rightmax = score.size() ;
                }
                int i = 0;
                int start = leftmin;
                vector<int> tmp;
                tmp.clear();

                while (rightmin < rightmax && leftmin < leftmax) {
                    if (score[leftmin] >= score[rightmin]) {
                        tmp.push_back(score[leftmin++]);
                    } else
                        tmp.push_back(score[rightmin++]);
                }

                while (leftmin < leftmax) {
                    tmp.push_back(score[leftmin++]);
                }
                while (rightmin < rightmax) {
                    tmp.push_back(score[rightmin++]);
                }
                for (int i = 0; i < tmp.size(); i++) {
                    score[start + i] = tmp[i];
                }
            }

            leftmin = rightmax;
        }

        for (int i = 0; i < score.size(); i++) {
            if ((i + 1) == 1)
                scoreRank[score[i]] = "Gold Medal";
            else if ((i + 1) == 2)
                scoreRank[score[i]] = "Silver Medal";
            else if ((i + 1) == 3)
                scoreRank[score[i]] = "Bronze Medal";
            else
                scoreRank[score[i]] = to_string(i + 1);
        }
        for (int i = 0; i < temp.size(); i++) {
            answer[i] = scoreRank[temp[i]];
        }
        return answer;
    }
};

请添加图片描述

我看没有人用归并排序的迭代方法做吧…


focus:
1 rightmax设定取不到的值
2 step 加上step也是取不到的值

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值