11.盛水最多容器
class Solution:
def maxArea(self, height: List[int]): -> int:\
left = 0
right = len(height) - 1
while left<right:
area = min(height[left], height[right]) * (right - left)
ans = max(ans, area)
if height[left]<= height[right]:
left += 1
else:
right -= 1
return ans
14.最长公共前缀
class solution:
def longestCommonPrefix(self, strs:List[str]) -> str:
if not strs:
return ""
length, count = len(str[0]), len(strs)
for i in range(length):
c = strs[0][i]
if any(i == len(strs[j]) or strs[j][i] != c for j in range(1, count)):
return strs[0][:i]
return strs[0]
15.三数之和相加为0
class solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
n = len(nums)
nums.sort()
ans = list()
#枚举a
for first in range(n):
if first > 0 and nums[first] == nums[first - 1]:
continue
#c对应的指针初始指向数组最右端
third = n-1
target = -nums[first]
#枚举b
for second in range(first + 1, n):
if second > first+1 and nums[second] == nums[second - 1]:
continue
while second < third and nums[second] + nums[third] > target:
third -= 1
if second == third:
break
if nums[second] + nums[third] = target:
ans.append([nums[first], nums[second], nums[third])
return ans