力扣题:数字与字符串间转换-12.11

力扣题-12.11

[力扣刷题攻略] Re:从零开始的力扣刷题生活

力扣题1:506. 相对名次

解题思想:进行排序即可

力扣题:数字与字符串间转换-12.11_第1张图片

class Solution(object):
    def findRelativeRanks(self, score):
        """
        :type score: List[int]
        :rtype: List[str]
        """
        n=len(score)
        answer=[0]*n
        score1 = 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;
    }
};

你可能感兴趣的:(leetcode,算法,职场和发展)