当前位置: 首页 > news >正文

Python 06 Set

Python 实例教程

  • Python 实例教学_ 06_集合
  • 第二十八课
    • [2351. 第一个出现两次的字母](https://leetcode.cn/problems/first-letter-to-appear-twice/)
    • [217. 存在重复元素](https://leetcode.cn/problems/contains-duplicate/)
    • [219. 存在重复元素 II](https://leetcode-cn.com/problems/contains-duplicate-ii/)
  • 第二十九课
    • [268. 丢失的数字](https://leetcode.cn/problems/missing-number/)
    • [929. 独特的电子邮件地址](https://leetcode.cn/problems/unique-email-addresses/)
    • [2215. 找出两数组的不同](https://leetcode.cn/problems/find-the-difference-of-two-arrays/)
  • 第三十课
    • [575. 分糖果](https://leetcode.cn/problems/distribute-candies/)
    • [1684. 统计一致字符串的数目](https://leetcode.cn/problems/count-the-number-of-consistent-strings/)
  • [2460. 对数组执行操作](https://leetcode.cn/problems/apply-operations-to-an-array/)
  • [1710. 卡车上的最大单元数](https://leetcode.cn/problems/maximum-units-on-a-truck/)

Python 实例教学_ 06_集合

Python 1-19 集合

第二十八课

2351. 第一个出现两次的字母

class Solution:def repeatedCharacter(self, s: str) -> str:st = set()for c in s:if c in st: return cst.add(c)

217. 存在重复元素

class Solution:def containsDuplicate(self, nums: List[int]) -> bool:d = defaultdict(int)for x in nums:if x in d:return Trued[x] += 1return Falses = set()for x in nums:if x in s:return Trues.add(x)return Falsereturn len(set(nums)) != len(nums)

219. 存在重复元素 II

class Solution:def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:# 1、双 for 超时# n = len(nums)# for i in range(n):#     for j in range(i + 1, n):#         if nums[i] == nums[j] and j - i <= k:#             return True# return False# 2、dictd = defaultdict(int)for i, x in enumerate(nums):if x in d and i - d[x] <= k: return Trued[x] = ireturn False# 3、sets, left = set(), 0# for i, x in enumerate(nums):for x in nums:if x in s: return Trues.add(x)# if len(s) > k: s.remove(nums[i - k])if len(s) > k: s.remove(nums[left])left += 1           return False

第二十九课

268. 丢失的数字

class Solution:def missingNumber(self, nums: List[int]) -> int:## 方法一:排序# n = len(nums)# nums.sort()# for i in range(n):#     if nums[i] != i:#         return i# return n## 方法二:哈希表# hash = set(nums)# for i in range(len(nums) + 1):#     if i not in hash:#         return i## 方法三:差# return sum(range(len(nums) + 1)) - sum(nums)# return (n := len(nums)) * (n + 1) // 2 - sum(nums)## 方法四:异或res = 0for i, num in enumerate(nums):res ^= i ^ numres ^= len(nums)return res

929. 独特的电子邮件地址

class Solution:def numUniqueEmails(self, emails: List[str]) -> int:emailSet = set()for email in emails:i = email.index('@')local = email[:i].split('+', 1)[0]  # 去掉本地名第一个加号之后的部分local = local.replace('.', '')  # 去掉本地名中所有的句点emailSet.add(local + email[i:])return len(emailSet)

2215. 找出两数组的不同

class Solution:def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:a, b = set(nums1), set(nums2)return [list(a - b), list(b - a)]

第三十课

575. 分糖果

class Solution:def distributeCandies(self, candyType: List[int]) -> int:n = len(candyType) // 2s = set(candyType)if len(s) >= n: return nreturn len(s)# return min(len(candyType)//2, len(set(candyType)))

1684. 统计一致字符串的数目

class Solution:def countConsistentStrings(self, allowed: str, words: List[str]) -> int:res = 0for w in words: # 外循环# flag = True# for c in w: # 内循环#     if c not in allowed:#         flag = False#         break # 终止(内)循环# if flag: res += 1# for c in w: # 内循环#     if c not in allowed:                    #         break # 终止(内)循环# else: res += 1 # 这个是和 for 对齐的,跳过 break 终止的循环res += all(c in allowed for c in w)return resreturn sum(all(c in allowed for c in w) for w in words)return sum(set(allowed) >= set(s) for s in words)

2460. 对数组执行操作

class Solution:def applyOperations(self, nums: List[int]) -> List[int]:n, left = len(nums), 0for i in range(n):if i + 1 < n and nums[i] == nums[i + 1]:nums[i], nums[i + 1] = nums[i] * 2, 0if nums[i]:nums[i], nums[left] = nums[left], nums[i]left += 1return nums

1710. 卡车上的最大单元数

class Solution:def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:boxTypes.sort(key=lambda x:-x[1])ans = 0for a, b in boxTypes:if a >= truckSize:ans += truckSize * bbreak            ans += a * btruckSize -= a            return ans
http://www.lryc.cn/news/447407.html

相关文章:

  • ControllerAdvice定义统一异常处理
  • Leetcode 162.寻找峰值
  • c语言:知识补充
  • Dapper介绍及特性
  • LeetCode 149. 直线上最多的点数
  • 案例研究丨国控星鲨利用DataEase释放数据潜能,重塑业务视野
  • 网络基础概念和 socket 编程
  • TypeScript 中的接口、泛型与自定义类型
  • 常州威雅学校:跟随这场音乐盛宴,溯回她的音乐之路
  • 【YashanDB知识库】由于hist_head$中analyze time小于tab$中analyze time导致的sql语句执行慢
  • 【有啥问啥】深度理解主动学习:机器学习的高效策略
  • 智能守护者X100 - 自动化生产线智能机器人安全监控管理系统
  • harmonyos面试题
  • 神经网络介绍及其在Python中的应用(一)
  • 数据流处理技术与Flink框架
  • qt中QTatlewidget类常用操作表格的函数有哪些?
  • Linux上的C/C++编程
  • 注意 秋季饮酒的正确打开方式
  • Python如何配置环境变量详解
  • Linux驱动开发(速记版)--并发与竞争
  • AI赋能,数字技术服务平台促进产业协同发展
  • RabbitMQ下载安装运行环境搭建
  • Redis过期时间删除策略详解
  • mysql数据库的基本管理
  • 根据现有html里的元素上面动态创建el-tooltip组件并显示的几种方式
  • 【C++篇】迈入新世界的大门——初识C++(上篇)
  • 啥?Bing搜索古早BUG至今未改?
  • ValueError: Out of range float values are not JSON compliant
  • 【架构】NewSQL
  • 禁止吸烟监测系统 基于图像处理的吸烟检测系统 YOLOv7