-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathac506.go
55 lines (45 loc) · 956 Bytes
/
ac506.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package problem506
import (
"sort"
"strconv"
)
type person struct {
score int
index int
}
type persons []person
// Len方法返回集合中的元素个数
func (p persons) Len() int {
return len(p)
}
// Less方法报告索引i的元素是否比索引j的元素小
func (p persons) Less(i, j int) bool {
// 此处实现从大到小排序 (大的数字在前,小的在后)
return p[i].score > p[j].score
}
// Swap方法交换索引i和j的两个元素
func (p persons) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func findRelativeRanks(nums []int) []string {
ans := make([]string, len(nums))
ps := make(persons, len(nums))
for i, v := range nums {
ps[i].score = v
ps[i].index = i
}
sort.Sort(ps)
for i, v := range ps {
switch i {
case 0:
ans[v.index] = "Gold Medal"
case 1:
ans[v.index] = "Silver Medal"
case 2:
ans[v.index] = "Bronze Medal"
default:
ans[v.index] = strconv.Itoa(i + 1)
}
}
return ans
}