上周的周赛,还是挺简单的
只需要保证【最小】<x<【最大】就可以了
考虑到数据量1 <= nums.length <= 100
,可以直接写成一行,否则需要存一下min和max
class Solution: def countElements(self, nums: List[int]) -> int: return sum(min(nums) < x < max(nums) for x in nums)
筛选+拼接,可以学习下chain和zip的用法
class Solution: def rearrangeArray(self, nums: List[int]) -> List[int]: return [*chain(*zip((x for x in nums if x > 0), (x for x in nums if x < 0)))]
Counter直接就过了
class Solution: def findLonely(self, nums: List[int]) -> List[int]: cnt = Counter(nums) return [i for i in cnt.keys() if not cnt[i+1] and not cnt[i-1] and cnt[i]==1]
二进制枚举,2行解法
class Solution: def maximumGood(self, statements: List[List[int]]) -> int: cnt=lambda i:0 if any((i >> j) & 1 and st < 2 and st != (i >> k) & 1 for j, row in enumerate(statements) for k, st in enumerate(row)) else bin(i).count('1') return max(cnt(i) for i in range(1, 1 << len(statements)))
偶然看到的一个大神的真·一行解法