本文主要是介绍力扣题:数字与字符串间转换-12.11,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
力扣题-12.11
[力扣刷题攻略] Re:从零开始的力扣刷题生活
力扣题1:506. 相对名次
解题思想:进行排序即可
class Solution(object):def findRelativeRanks(self, score):""":type score: List[int]:rtype: List[str]"""n=len(score)answer=[0]*nscore1 = sorted(score, reverse=True)for i in range(n):k=score.index(score1[i])if i==0:answer[k]='Gold Medal'elif i==1:answer[k]='Silver Medal'elif i==2:answer[k]='Bronze Medal'else:answer[k]=str(i+1)return answer
class Solution {
public:vector<string> findRelativeRanks(vector<int>& score) {int n = score.size();std::vector<std::string> answer(n);std::vector<int> scoreCopy = score;std::sort(scoreCopy.rbegin(), scoreCopy.rend());for (int i = 0; i < n; ++i) {auto it = std::find(score.begin(), score.end(), scoreCopy[i]);int k = std::distance(score.begin(), it);if (i == 0) {answer[k] = "Gold Medal";} else if (i == 1) {answer[k] = "Silver Medal";} else if (i == 2) {answer[k] = "Bronze Medal";} else {answer[k] = std::to_string(i + 1);}}return answer;}
};
这篇关于力扣题:数字与字符串间转换-12.11的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!