★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-mlqnicuv-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
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".git
Example 1:github
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:微信
給出 N 名運動員的成績,找出他們的相對名次並授予前三名對應的獎牌。前三名運動員將會被分別授予 「金牌」,「銀牌」 和「 銅牌」("Gold Medal", "Silver Medal", "Bronze Medal")。app
(注:分數越高的選手,排名越靠前。)spa
示例 1:code
輸入: [5, 4, 3, 2, 1] 輸出: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"] 解釋: 前三名運動員的成績爲前三高的,所以將會分別被授予 「金牌」,「銀牌」和「銅牌」 ("Gold Medal", "Silver Medal" and "Bronze Medal"). 餘下的兩名運動員,咱們只須要經過他們的成績計算將其相對名次便可。
提示:htm
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 }
48msblog
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 }