pseudocode - 计算数组中存在多少“倒计时”序列
问题描述
我知道这听起来很简单,但我正在尝试获取数组中存在的“倒计时”序列的计数。示例:[1,2,3,2,5,4,3,0] - > 2 ([3,2] 和 [5,4,3]) 我只需要一点推力,拜托!
解决方案
每次倒计时中断时,只需遍历列表即可递增计数器
Python,也称为伪代码:
def count_finder(l):
prev = l[0]
counter = 0
inCount = False
for num in l[1:]:
if num == prev-1: #Checks if the previous was 1 greater than this one
inCount = True # if it is then "inCount" is True
elif num+1 != prev and inCount: #Checks if your exiting a countdown
inCount = False
counter += 1 #Increment Counter
prev = num #Change previous number to current number for next loop
if inCount: counter+=1 #If the loop ends while in a count down increment counter
return counter
print(count_finder([9, 8, 7, 6, 5, 4]))
推荐阅读
- arrays - 如何在swift中按日期对字典数组中的项目进行排序
- html - 如何使用 flexbox 在整个页面上拉伸标题并使用响应屏幕状态
- r - 删除 R 中重复项不准确的行
- spring-boot - 如何在spring boot中使用http补丁请求?
- jquery - 鼠标事件名称
- sql - Invoke-DbaQuery 失败 - 等待操作超时
- java - 我的 Havel-Hakimi 算法代码有什么问题?
- git - 我们如何轻松解决由于 React 应用程序中的文件转译导致的合并冲突?
- azure - 使用 Azure Front Door 提供 SPA
- java - 在 Java 中获取 TreeSet 的 headSet 的时间复杂度是多少?另外,如果我调用 headSet 方法'n'次怎么办?