小白学数据,只为记录学习进程,对每个问题有新的理解会及时更正。
剑指 Offer 57 - II. 和为s的连续正数序列
想法:滑动窗口,改变窗口的上下限来遍历
class Solution:
def findContinuousSequence(self, target: int) -> List[List[int]]:
i,j,res = 1,2,[]
while j <= target//2 + 1:
cursum = sum(list(range(i,j+1)))
if cursum == target:
res.append(list(range(i,j+1)))
j += 1
elif cursum < target:j+=1
else:i += 1
return res
剑指 Offer 61. 扑克牌中的顺子
思路:判断最大值和最小值是否小于5就可
class Solution:
def isStraight(self, nums: List[int]) -> bool:
res = []
mi,ma = 14,0
for i in nums:
if i in res:return False
if i == 0:continue
mi = min(i,mi)
ma = max(i,ma)
res.append(i)
return ma - mi < 5