File tree Expand file tree Collapse file tree 3 files changed +94
-5
lines changed
solution/0500-0599/0506.Relative Ranks Expand file tree Collapse file tree 3 files changed +94
-5
lines changed Original file line number Diff line number Diff line change 25
25
<li>所有运动员的成绩都不相同。</li>
26
26
</ol >
27
27
28
-
29
28
## 解法
30
29
31
30
<!-- 这里可写通用的实现逻辑 -->
37
36
<!-- 这里可写当前语言的特殊实现逻辑 -->
38
37
39
38
``` python
40
-
39
+ class Solution :
40
+ def findRelativeRanks (self , score : List[int ]) -> List[str ]:
41
+ n = len (score)
42
+ idx = list (range (n))
43
+ idx.sort(key = lambda x : - score[x])
44
+ res = [None ] * n
45
+ for i in range (n):
46
+ if i == 0 :
47
+ res[idx[i]] = ' Gold Medal'
48
+ elif i == 1 :
49
+ res[idx[i]] = ' Silver Medal'
50
+ elif i == 2 :
51
+ res[idx[i]] = ' Bronze Medal'
52
+ else :
53
+ res[idx[i]] = str (i + 1 )
54
+ return res
41
55
```
42
56
43
57
### ** Java**
44
58
45
59
<!-- 这里可写当前语言的特殊实现逻辑 -->
46
60
47
61
``` java
48
-
62
+ class Solution {
63
+ public String [] findRelativeRanks (int [] nums ) {
64
+ int n = nums. length;
65
+ Integer [] index = new Integer [n];
66
+ for (int i = 0 ; i < n; ++ i) {
67
+ index[i] = i;
68
+ }
69
+ Arrays . sort(index, (o1, o2) - > Integer . compare(nums[o2], nums[o1]));
70
+ String [] res = new String [n];
71
+ for (int i = 0 ; i < n; ++ i) {
72
+ if (i == 0 ) {
73
+ res[index[i]] = " Gold Medal" ;
74
+ } else if (i == 1 ) {
75
+ res[index[i]] = " Silver Medal" ;
76
+ } else if (i == 2 ) {
77
+ res[index[i]] = " Bronze Medal" ;
78
+ } else {
79
+ res[index[i]] = String . valueOf(i + 1 );
80
+ }
81
+ }
82
+ return res;
83
+ }
84
+ }
49
85
```
50
86
51
87
### ** ...**
Original file line number Diff line number Diff line change 52
52
### ** Python3**
53
53
54
54
``` python
55
-
55
+ class Solution :
56
+ def findRelativeRanks (self , score : List[int ]) -> List[str ]:
57
+ n = len (score)
58
+ idx = list (range (n))
59
+ idx.sort(key = lambda x : - score[x])
60
+ res = [None ] * n
61
+ for i in range (n):
62
+ if i == 0 :
63
+ res[idx[i]] = ' Gold Medal'
64
+ elif i == 1 :
65
+ res[idx[i]] = ' Silver Medal'
66
+ elif i == 2 :
67
+ res[idx[i]] = ' Bronze Medal'
68
+ else :
69
+ res[idx[i]] = str (i + 1 )
70
+ return res
56
71
```
57
72
58
73
### ** Java**
59
74
60
75
``` java
61
-
76
+ class Solution {
77
+ public String [] findRelativeRanks (int [] nums ) {
78
+ int n = nums. length;
79
+ Integer [] index = new Integer [n];
80
+ for (int i = 0 ; i < n; ++ i) {
81
+ index[i] = i;
82
+ }
83
+ Arrays . sort(index, (o1, o2) - > Integer . compare(nums[o2], nums[o1]));
84
+ String [] res = new String [n];
85
+ for (int i = 0 ; i < n; ++ i) {
86
+ if (i == 0 ) {
87
+ res[index[i]] = " Gold Medal" ;
88
+ } else if (i == 1 ) {
89
+ res[index[i]] = " Silver Medal" ;
90
+ } else if (i == 2 ) {
91
+ res[index[i]] = " Bronze Medal" ;
92
+ } else {
93
+ res[index[i]] = String . valueOf(i + 1 );
94
+ }
95
+ }
96
+ return res;
97
+ }
98
+ }
62
99
```
63
100
64
101
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def findRelativeRanks (self , score : List [int ]) -> List [str ]:
3
+ n = len (score )
4
+ idx = list (range (n ))
5
+ idx .sort (key = lambda x : - score [x ])
6
+ res = [None ] * n
7
+ for i in range (n ):
8
+ if i == 0 :
9
+ res [idx [i ]] = 'Gold Medal'
10
+ elif i == 1 :
11
+ res [idx [i ]] = 'Silver Medal'
12
+ elif i == 2 :
13
+ res [idx [i ]] = 'Bronze Medal'
14
+ else :
15
+ res [idx [i ]] = str (i + 1 )
16
+ return res
You can’t perform that action at this time.
0 commit comments