leetcode 506. ????????????(Relative Ranks)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode 506. ????????????(Relative Ranks)相关的知识,希望对你有一定的参考价值。
?????????back toc char ?????? ?????? tostring ati str while
??????
???????????????
?????? N ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ??????????????????????????? ?????? ????????????"Gold Medal", "Silver Medal", "Bronze Medal"??????
(????????????????????????????????????????????????)
?????? 1:
??????: [5, 4, 3, 2, 1]
??????: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
??????: ???????????????????????????????????????????????????????????????????????? ?????????????????????????????????????????? ("Gold Medal", "Silver Medal" and "Bronze Medal").
????????????????????????????????????????????????????????????????????????????????????????????????
??????:
- N ???????????????????????????????????? 10000???
- ???????????????????????????????????????
?????????
# define PR pair<int, int>
class Solution {
public:
string toString(int num){
if(num == 1){
return "Gold Medal";
}else if(num == 2){
return "Silver Medal";
}else if(num == 3){
return "Bronze Medal";
}else{
string res = "";
while(num != 0){
res = char(num%10 + '0') + res;
num /= 10;
}
return res;
}
}
vector<string> findRelativeRanks(vector<int>& nums) {
int sz = nums.size();
vector<PR> lst;
for(int i = 0; i < sz; i++){
lst.push_back(make_pair(-nums[i], i));
}
sort(lst.begin(), lst.end());
vector<string> res(sz, "");
for(int i = 0; i < sz; i++){
// cout<<lst[i].first<<", "<<lst[i].second<<endl;
res[lst[i].second] = toString(i+1);
}
return res;
}
};
以上是关于leetcode 506. ????????????(Relative Ranks)的主要内容,如果未能解决你的问题,请参考以下文章
[LeetCode&Python] Problem 506. Relative Ranks
[LeetCode] 506. Relative Ranks
leetcode 506. ????????????(Relative Ranks)