1.leetcode455
class Solution(object):
def findContentChildren(self, g, s):
"""
:type g: List[int]
:type s: List[int]
:rtype: int
"""
g.sort()
s.sort()
i=j =0
cnt = 0
while i<len(g) and j<len(s):
if g[i] > s[j]:
j +=1
else:
i +=1
j +=1
cnt +=1
return cnt
2、leetcode376
class Solution(object):
def wiggleMaxLength(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
cnt = l = len(nums)
i = 0
up = down = 0
while i+1<l:
if nums[i]>nums[i+1]:
if down ==1:
cnt -=1
down = 1
up = 0
elif nums[i]<nums[i+1]:
if up ==1:
cnt -=1
down=0
up =1
elif nums[i]==nums[i+1]:
cnt -=1
i+=1
return cnt
3、leetcode402
class Solution(object):
def removeKdigits(self, num, k):
"""
:type num: str
:type k: int
:rtype: str
"""
i=0
end = "0"
if len(num)<= k:
return end
num = num+'A'
while i<len(num)-1 and k>0:
i=0
while i<len(num)-1:
if num[i]>num[i+1]:
num = num[:i]+'-'+num[i+1:]
k-=1
break
i +=1
num = num.replace('-','')
#print(num)
num =num.replace('A','')
if k>0:
if num[0]>num[-1]:
num = num[:-1-k]+num[-1]
else:
num = num[:-k]
num = num.lstrip('0')
num =num.replace('A','')
num = num.replace('-','')
if num !="":
return num
return end
我要好好说说这个题 妈的改了一整天 嗯我知道我cai anyway 我的做法是直接从高位到低位删除 但是细节太多了 各种边界过不了 这个题主要注意:
1、要注意0的处理, 当然我用的是python 的lstrip 所以比较简单
2、要注意最后一个字符的比较问题,我选择在后面追加一个元素,所以最后要记住replace()掉,哦还有replace(),strip()都是返回一个新列表的,老不写容易忘
3、还有一个是当所有元素都遍历完成后,k>0的情况,这种情况说明序列部分(最后一个元素可能不是递增的)或全部是递增的,因此,k>0时,要注意分类,然后切片操作很快能得到结果。
这个题比较推荐的做法是贪心+栈,但是我不太熟悉栈,所以一开始没有想到用栈。下面贴上用栈的代码。用栈实现简单很多。
class Solution:
def removeKdigits(self, num: str, k: int) -> str:
s = []
ans = ""
i=1
s.append(num[0])
while i<len(num):
cur = num[i]
if cur > s[-1]:
s.append(cur)
else:
while len(s)>0 and k>0 and cur<s[-1]:
s.pop()
k -=1
s.append(cur)
i+=1
for i in s:
ans +=i
ans = ans.lstrip('0')
if k>0:
ans = ans[:-k]
if ans is not "":
return ans
return "0"
4、leetcode55
class Solution(object):
def canJump(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
l = len(nums)
if l==1:
return True
ans =[]
for i in range(0,l):
ans.append(i+nums[i])
maxindex = ans[0]
i=0
while i<l-1 and i<=maxindex: #循环条件是不能超过数组并且不能超过能够当前到达的位置
if ans[i]>maxindex:
maxindex = ans[i]
i +=1
print(maxindex)
if maxindex>=l-1:
return True
return False
呜呜呜呜呜我好想哭我好菜调了好久才过
5、leetcode45
class Solution(object):
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
l = len(nums)
if l==1:
return 0
ans =[]
for i in range(0,l):
ans.append(i+nums[i])
i=0
cnt =0
print(ans)
while i<l-1: #循环条件是不能超过数组并且不能超过能够当前到达的位置
cur = ans[i+1:ans[i]+1]
if ans[i]>=l-1:
cnt+=1
break
print(cur)
if max(cur)>=l-1:
cnt +=2
break
else:
i = cur.index(max(cur))+i+1
cnt+=1
print(i)
return cnt
上一题的变形。
6、leetcode452
#
# @lc app=leetcode.cn id=452 lang=python
#
# [452] 用最少数量的箭引爆气球
#
class Solution(object):
def findMinArrowShots(self, points):
"""
:type points: List[List[int]]
:rtype: int
"""
points.sort()
l = len(points)
if l<=1:
return l
#start = points[0][0]
end = points[0][1]
cnt = 1
for i in range(1,l):
if points[i][0] <=end:
if points[i][1]<=end:
end = points[i][1]
else:
end = points[i][1]
cnt +=1
return cnt