[Python]A stupid question / 据说是某前端面试题

对于给定list:

1、若最高位个数大于2 ,返回最高位

2、若最高位个数不大于2,返回最高位和次高位

example:

'''
[5,4,3,2,1] -> [5,4]
[5,5,4,4,4] -> [5,5]
[5,5,5,4,4] -> [5,5,5]
[5,5,5,5,5] -> [5,5,5,5,5]
[5,4,4,3,2] -> [5,4,4]
[5,4,4,4,3] -> [5,4,4,4]
[5,4,4,4,4] -> [5,4,4,4,4]
'''
 1 '''
 2 [5,4,3,2,1] -> [5,4]
 3 [5,5,4,4,4] -> [5,5]
 4 [5,5,5,4,4] -> [5,5,5]
 5 [5,5,5,5,5] -> [5,5,5,5,5]
 6 [5,4,4,3,2] -> [5,4,4]
 7 [5,4,4,4,3] -> [5,4,4,4]
 8 [5,4,4,4,4] -> [5,4,4,4,4]
 9 '''
10 class Solution:
11     def isMatch(self, s: list) -> list:
12         lens = len(s)
13         # 0
14         if lens == 0:
15             return []
16         #1,2
17         if lens <= 2:
18             return s
19         #normal
20         s.sort()
21         max_1 = s[-1]
22         countmax_1 = s.count(s[-1])
23         #[5,5,5,5,5]
24         if countmax_1 < lens:
25             max_2 = s[-(1 + countmax_1)]
26             countmax_2 = s.count(s[-(1 + countmax_1)])
27         #return
28         if countmax_1 >=2:
29             return s[ (lens - countmax_1):]
30         else:
31             return s [(lens - countmax_1 -countmax_2):][::-1]
32 def main():
33     import sys
34     #import io
35 
36     s = [
37         [5,4,3,2,1],
38         [5,5,4,4,4],
39         [5,5,5,4,4],
40         [5,5,5,5,5],
41         [5,4,4,3,2],
42         [5,4,4,4,3],
43         [5,4,4,4,4]
44     ]
45 
46     for i in s:
47         ret = Solution().isMatch(i)
48         print(ret)
49 
50 if __name__ == '__main__':
51     main()
原文地址:https://www.cnblogs.com/alfredsun/p/13870087.html