1. 缺失的第一个正数
题目链接:https://leetcode.cn/problems/first-missing-positive/
给定一个未排序的整数数组 nums ,请找出其中没有出现的最小的正整数。
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
n = len(nums)
for i in range(n):
while 1 <= nums[i] <= n and nums[nums[i] - 1]!= nums[i]:
nums[nums[i] - 1], nums[i] = nums[i], nums[nums[i] - 1]
for i in range(n):
if nums[i]!= i + 1:
return i + 1
return n + 1
2. 接雨水
题目链接:https://leetcode.cn/problems/trapping-rain-water/
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
class Solution:
def trap(self, height: List[int]) -> int:
left, right = 0, len(height) - 1
left_max, right_max = 0, 0
res = 0
while left < right:
if height[left] < height[right]:
if height[left] >= left_max:
left_max = height[left]
else:
res += left_max - height[left]
left += 1
else:
if height[right] >= right_max:
right_max = height[right]
else:
res += right_max - height[right]
right -= 1
return res
标签:right,int,max,Study,height,Algorithms,Plan,res,left
From: https://www.cnblogs.com/stephenxiong001/p/18397440