11、盛最多水的容器
class Solution:
def maxArea(self, height: List[int]) -> int:
left = 0
right = len(height) - 1
maxArea = 0
while left < right:
b = right - left
if height[left] < height[right]:
h = height[left]
left += 1
else:
h = height[right]
right -= 1
area = b*h
if maxArea < area:
maxArea = area
return maxArea
14、最长公共前缀
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs: return ""
s1 = min(strs)
s2 = max(strs)
for i,x in enumerate(s1):
if x != s2[i]:
return s2[:i]
return s1
15、三数之和
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort();
ans=set();
target=0;
for i in range(len(nums)-2):
left=i+1;
right=len(nums)-1;
while(right>left):
temp=nums[i]+nums[left]+nums[right];
if target==temp:
ans.add((nums[i],nums[left],nums[right]));
left=left+1;
right=right-1;
elif temp>target:
right=right-1;
elif temp<target:
left=left+1;
rec=[];
for i in ans:
rec.append(list(i));
return rec;