leetcode 506. Relative Ranks 运动员名次
2017-12-12 10:00
363 查看
Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: “Gold Medal”, “Silver Medal” and “Bronze Medal”.
Example 1:
Input: [5, 4, 3, 2, 1]
Output: [“Gold Medal”, “Silver Medal”, “Bronze Medal”, “4”, “5”]
Explanation: The first three athletes got the top three highest scores, so they got “Gold Medal”, “Silver Medal” and “Bronze Medal”.
For the left two athletes, you just need to output their relative ranks according to their scores.
本题题意很简单,就是做一个简单的排序
代码如下:
Example 1:
Input: [5, 4, 3, 2, 1]
Output: [“Gold Medal”, “Silver Medal”, “Bronze Medal”, “4”, “5”]
Explanation: The first three athletes got the top three highest scores, so they got “Gold Medal”, “Silver Medal” and “Bronze Medal”.
For the left two athletes, you just need to output their relative ranks according to their scores.
本题题意很简单,就是做一个简单的排序
代码如下:
#include <iostream> #include <vector> #include <map> #include <set> #include <queue> #include <stack> #include <string> #include <climits> #include <algorithm> #include <sstream> #include <functional> #include <bitset> #include <cmath> using namespace std; bool cmp(int a, int b) { return a > b; } class Solution { public: vector<string> findRelativeRanks(vector<int>& nums) { vector<string> res; vector<int> a = nums; sort(a.begin(), a.end(),cmp); for (int i : nums) res.push_back(getRank(i, a)); return res; } string getRank(int target, vector<int> a) { int i = 0; for (i = 0; i < a.size(); i++) { if (a[i] == target) break; } if (i == 0) return "Gold Medal"; else if (i == 1) return "Silver Medal"; else if (i == 2) return "Bronze Medal"; else return to_string(i + 1); } };
相关文章推荐
- 5位运动员参加10米台跳水比赛,对他们名次做出预测。
- 逻辑题:确定5位运动员的比赛名次。
- 5名运动员跳水比赛的名次问题
- 5位运动员参加了10米台跳水比赛,有人让他们预测比赛结果,比赛结束后,每位选手都说对了一半,请编程确定比赛的名次
- c语言:2种方法:5位运动员参加跳水比赛,每位选手都说对一半,请确定比赛名次
- 5位运动员参加了10米台跳水比赛,每位选手都说对了一半,请编程确定比赛的名次
- c语言:2种方法:5位运动员参加跳水比赛,每位选手都说对一半,请确定比赛名次
- 5位运动员参加了10米台跳水比赛,有人让他们预测比赛结果 确定比赛的名次
- 各种小程序:(运动员名次与谁是凶手等)
- [LeetCode] Largest Number
- LeetCode OJ Binary Tree Level Order Traversal II
- 【leetcode】Intersection of Two Arrays
- [LeetCode刷题笔记]Math数学类型题目(三)特殊的数字结构
- Leetcode:Swap Nodes in Pairs 单链表相邻两节点逆置
- Leetcode 343(Integer Break非动态规划求解)
- LeetCode-Palindrome Number
- leetcode之Isomorphic Strings
- LeetCode 292 Nim Game(Nim游戏)
- (java)leetcode-21
- [leetcode]: 342. Power of Four