教日月换新天。为有牺牲多壮志,敢

[Swift]LeetCode506. 相对名次 | Relative Ranks

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9810383.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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.

 Note:

  1. N is a positive integer and won't exceed 10,000.
  2. All the scores of athletes are guaranteed to be unique.

 给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌。前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”("Gold Medal", "Silver Medal", "Bronze Medal")。

(注:分数越高的选手,排名越靠前。)

示例 1:

输入: [5, 4, 3, 2, 1]
输出: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
解释: 前三名运动员的成绩为前三高的,因此将会分别被授予 “金牌”,“银牌”和“铜牌” ("Gold Medal", "Silver Medal" and "Bronze Medal").
余下的两名运动员,我们只需要通过他们的成绩计算将其相对名次即可。

提示:

  1. N 是一个正整数并且不会超过 10000。
  2. 所有运动员的成绩都不相同。

复制代码
 1 class Solution {
 2     func findRelativeRanks(_ nums: [Int]) -> [String] {
 3         var arr:[Int] = nums
 4         //降序排序
 5         arr = arr.sorted(by: >)
 6         let len:Int = nums.count
 7         var res:[String] = Array(repeating: "",count: len)
 8 
 9         //从nums中寻找与arr[j]相同元素,在res中对应位置赋值为j+1, j=0,1,2时特殊处理
10         for i in 0..<len
11         {
12             for j in 0..<len
13             {
14                 if nums[i] == arr[j]
15                 {
16 //整个switch语句在第一个匹配switch案例完成后立即完成其执行,而不需要显式break语句。  
17 //尽管break在Swift中不需要,但您可以使用break语句来匹配和忽略特定情况,
18 //或者在该情况完成执行之前中断匹配的情况。  
19                     switch j
20                     {
21                         case 0:
22                         res[i] = "Gold Medal"
23                         case 1:
24                         res[i] = "Silver Medal"
25                         case 2:
26                         res[i] = "Bronze Medal"           
27                         default:
28                         res[i] = String(j + 1)                        
29                     }
30                  break
31                 }              
32             }
33         }
34         return res
35     }
36 }
复制代码

48ms

复制代码
 1 class Solution {
 2     func findRelativeRanks(_ nums: [Int]) -> [String] {
 3         guard nums.count > 0 else {
 4             return []
 5         }
 6         var index = [Int:Int]()
 7         for i in 0..<nums.count {
 8             index[nums[i]] = i
 9         }
10         let sorted = nums.sorted(by: >)
11         var rank = [String](repeating: "", count: nums.count)
12         for i in 0..<sorted.count {
13             if let idx = index[sorted[i]] {
14                 if i == 0 {
15                     rank[idx] = "Gold Medal" 
16                 } else if i == 1 {
17                     rank[idx] = "Silver Medal"
18                 } else if i == 2 {
19                     rank[idx] = "Bronze Medal"
20                 } else {
21                     rank[idx] = String(i+1)
22                 }
23             }
24 
25         }
26         return rank
27     }
28 }
复制代码

44ms

复制代码
 1 class Solution {
 2     func findRelativeRanks(_ nums: [Int]) -> [String] {
 3         let arr = nums.sorted(by: >)
 4         var res = [String]()
 5         var dict = [Int:String]()
 6         for i in 0..<arr.count {
 7             if i == 0 {
 8                 dict[arr[i]] = "Gold Medal"
 9             } else if i == 1 {
10                 dict[arr[i]] = "Silver Medal"
11             } else if i == 2 {
12                 dict[arr[i]] = "Bronze Medal"
13             } else {
14                 dict[arr[i]] = "\(i+1)"
15             }
16         }
17         for i in nums {
18             res.append(dict[i]!)
19         }
20         return res
21     }
22 }
复制代码

80ms

复制代码
 1 class Solution {
 2     func findRelativeRanks(_ nums: [Int]) -> [String] {
 3         var nums2 = nums.sorted()
 4         return nums.map({ (num) -> String in
 5             var left = 0
 6             var right = nums.count - 1
 7             while left < right {
 8                 let mid = left + (right - left) / 2
 9                 let val = nums2[mid]
10                 if val > num {
11                     right = mid
12                 } else if val < num {
13                     left = mid + 1
14                 } else {
15                     left = mid
16                     break
17                 }
18             }
19             
20             var string = ""
21             if left == nums.count - 1 {
22                 string = "Gold Medal"
23             } else if left == nums.count - 2 {
24                 string = "Silver Medal"
25             } else if left == nums.count - 3 {
26                 string = "Bronze Medal"
27             } else {
28                 string = String(nums.count - left)
29             }
30             return string
31         })
32     }
33 }
复制代码

172ms

复制代码
1 class Solution {
2     func findRelativeRanks(_ nums: [Int]) -> [String] {
3         let sortedNums = nums.sorted().reversed()
4         var ranks = ["Gold Medal", "Silver Medal", "Bronze Medal"]
5         ranks = nums.count > 3 ? ranks + (4..<nums.count+1).map{ return String($0) } : ranks
6         let dict = Dictionary(uniqueKeysWithValues: zip(sortedNums, ranks))
7         return nums.map{ return dict[$0]! }
8     }
9 }
复制代码

 

posted @   为敢技术  阅读(309)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°