【算法】力扣第 277 场周赛
2022/1/30 14:05:00
本文主要是介绍【算法】力扣第 277 场周赛,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
文章目录
- [2148. 元素计数](https://leetcode-cn.com/problems/count-elements-with-strictly-smaller-and-greater-elements/)
- [2149. 按符号重排数组](https://leetcode-cn.com/problems/rearrange-array-elements-by-sign/)
- [2150. 找出数组中的所有孤独数字](https://leetcode-cn.com/problems/find-all-lonely-numbers-in-the-array/)
- [2151. 基于陈述统计最多好人数](https://leetcode-cn.com/problems/maximum-good-people-based-on-statements/)
- 总结
上周的周赛,还是挺简单的
2148. 元素计数
只需要保证【最小】<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)
2149. 按符号重排数组
筛选+拼接,可以学习下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)))]
2150. 找出数组中的所有孤独数字
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]
2151. 基于陈述统计最多好人数
二进制枚举,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)))
偶然看到的一个大神的真·一行解法
这篇关于【算法】力扣第 277 场周赛的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)