排序+哈希表
排序:提前处理前三名之后的排序
class Solution:
def findRelativeRanks(self, score: List[int]) -> List[str]:
rank = sorted(score, reverse=True)
re = {}
for i in range(len(score)):
re[rank[i]] = i
res = []
for i in score:
if re[i]==0:
res.append("Gold Medal")
elif re[i]==1:
res.append("Silver Medal")
elif re[i]==2:
res.append("Bronze Medal")
else:
res.append(str(re[i]+1))
return res
将数组从小到大进行排序,a1<b1<a2<b2.....,a1是数组中最小数,无论给a1搭配任何数,a1都会被选中(min(a1,other)=a1),所以为了能够使得最终结果最大,给a1搭配次小数,以此类推。
class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums.sort()
return sum(nums[::2])
class Solution:
def findWords(self, words: List[str]) -> List[str]:
set1 = set('qwertyuiop')
set2 = set('asdfghjkl')
set3 = set('zxcvbnm')
res = []
for i in words:
x = set(i.lower())
if x&set1==x or x&set2==x or x&set3==x:
res.append(i)
return res