class Solution:
def partitionLabels(self, s: str) -> List[int]:
last_occurrence = {} # 存储每个字符最后出现的位置
for index, ch in enumerate(s):
last_occurrence[ch] = i
result = []
start = 0
end = 0
for index, ch in enumerate(s):
end = max(end, last_occurrence[ch]) # 找到当前字符出现的最远位置
if index == end: # 如果当前位置是最远位置,表示可以分割出一个区间
result.append(end - start + 1)
start = index + 1
return result
技巧【96-100】
136.只出现一次的数字
空间常数,位运算
python复制代码
class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
x = 0
for num in nums: # 1. 遍历 nums 执行异或运算
x ^= num
return x # 2. 返回出现一次的数字 x
169.多数元素
python复制代码
'''
======当发生 票数和 =0 时,剩余数组的众数一定不变 =====
如果vote为0,当前元素为临时众数
如果临时众数是全局众数,抵消的数字里面,一半是众数;没抵消的数组里面,众数肯定不变
如果临时众数不是全局众数,vito会变成0
'''
class Solution:
def majorityElement(self, nums: List[int]) -> int:
vote = 0
for i in nums:
if vote == 0:
x = i #众数是i
if i == x:
vote += 1
else :
vote -= 1
return x
'''
nums[0...zero] = 0 ;
nums[zero+1...i-1] = 1 ;
nums[two...n-1] = 2
'''
class Solution:
def sortColors(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
i, zero, two = 0,-1, len(nums)
while i < two:
if nums[i] == 1:
i += 1
elif nums[i] == 2:
two -= 1
nums[i], nums[two] = nums[two], nums[i]
else:
zero += 1
nums[i], nums[zero] = nums[zero], nums[i] #nums[zero]=1
i += 1
31.下一个排列
python复制代码
class Solution:
def nextPermutation(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
length = len(nums)
for i in range(length-2,-1,-1):
if nums[i] >= nums[i+1]:
continue #剪枝
for j in range(length-1,i,-1):
if nums[j] > nums[i]:
nums[j], nums[i] = nums[i], nums[j]
self.reverse(nums, i+1, length-1)
return
self.reverse(nums, 0, length-1) #代表是,降序排列
#翻转nums[left...... right]
def reverse(self, nums, left, right):
while left < right:
nums[left], nums[right] = nums[right], nums[left]
left += 1
right -= 1
287.寻找重复数
环形链表
python复制代码
class Solution:
def findDuplicate(self, nums: List[int]) -> int:
def next(i):
return nums[i]
slow = fast = 0
while True:
slow = next(slow)
fast = next(next(fast))
if slow == fast:
break
slow = 0
while slow != fast:
slow = next(slow)
fast = next(fast)
return slow #入环的地方
哈希表
python复制代码
class Solution:
def findDuplicate(self, nums: List[int]) -> int:
hmap = set()
for num in nums:
if num in hmap:
return num
else:
hmap.add(num)
return -1