Leetcode腾讯精选练习50题
11-盛最多水的容器
果然暴力求解是行不通的嘤嘤嘤
class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
return max([min(height[i],height[j])*(j-i) for i in range(len(height)-1) for j in range(i+1,len(height))])
看了评论区才晓得居然用的是DP,我以为最机智也就剪剪枝之类的呢…
贴个题解下周看!
顺带在此立下学会DP的flag!记得来拔啊朋友!
14-最长公共前缀
遇到简单题非常开心
- 如果输入为 [""],返回 “”
- 如果输入的字符串列表中任意元素为 “” ,返回 “”
- 公共前缀长度不超过输入的字符串列表中最短的那一个
- 循环判断一下就好了
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if len(strs)==0:
return ""
l=float('INF')
for s in strs:
if len(s)==0:
return ""
if len(s)<l:
l=len(s)
lst=""
i=0
flag=True
c=""
while flag:
lst+=c
if i==l:
break
c=strs[0][i]
for j in range(1,len(strs)):
if strs[j][i]!=c:
flag=False
break
i+=1
return lst
仍有提升空间
官方题解提供了四种思路,给看傻眼了。
我的做法属于第二种,纵向扫描,内存消耗较大应该是因为我整了个INF
15-三数之和
暴力解法果然不可
官方题解是排序+双指针,还挺有道理的,也不难理解