Python 实例教程
- Python 实例教学_ 06_集合
- 第二十八课
- 888. 公平的糖果棒交换
- [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/)
- ★575. 分糖果
- [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 集合
第二十八课
888. 公平的糖果棒交换
Leetcode知识点: sum set 总体差的一半通过交换重新分配
class Solution:
def fairCandySwap(self, aliceSizes: List[int], bobSizes: List[int]) -> List[int]:
a, b = sum(aliceSizes), sum(bobSizes)
c, d = aliceSizes, set(bobSizes)
for i in c:
j = (b - a)//2 + i
if j in d:
return [i, j]
2351. 第一个出现两次的字母
class Solution:
def repeatedCharacter(self, s: str) -> str:
st = set()
for c in s:
if c in st: return c
st.add(c)
217. 存在重复元素
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
d = defaultdict(int)
for x in nums:
if x in d:
return True
d[x] += 1
return False
s = set()
for x in nums:
if x in s:
return True
s.add(x)
return False
return 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、dict
d = defaultdict(int)
for i, x in enumerate(nums):
if x in d and i - d[x] <= k: return True
d[x] = i
return False
# 3、set
s, left = set(), 0
# for i, x in enumerate(nums):
for x in nums:
if x in s: return True
s.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 = 0
for i, num in enumerate(nums):
res ^= i ^ num
res ^= 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) // 2
s = set(candyType)
if len(s) >= n: return n
return len(s)
# return min(len(candyType)//2, len(set(candyType)))
★575. 分糖果
Leetcode知识点: 集合 set set.add,去重功能,无序,分类。// 整除、/ 除法。
class Solution:
def distributeCandies(self, candyType: List[int]) -> int:
n = len(candyType)//2
s = set()
for c in candyType:
s.add(c)
return len(s) if len(s) <= n else n
#return min(len(candyType)//2, len(set(candyType)))
1684. 统计一致字符串的数目
class Solution:
def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
res = 0
for 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 res
return 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), 0
for i in range(n):
if i + 1 < n and nums[i] == nums[i + 1]:
nums[i], nums[i + 1] = nums[i] * 2, 0
if nums[i]:
nums[i], nums[left] = nums[left], nums[i]
left += 1
return nums
1710. 卡车上的最大单元数
class Solution:
def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:
boxTypes.sort(key=lambda x:-x[1])
ans = 0
for a, b in boxTypes:
if a >= truckSize:
ans += truckSize * b
break
ans += a * b
truckSize -= a
return ans