413. Arithmetic Slices
来源:互联网 发布:粒子群算法工具箱 编辑:程序博客网 时间:2024/06/05 02:47
class Solution(object):
def numberOfArithmeticSlices(self, A):
"""
:type A: List[int]
:rtype: int
"""
opt, i = [0,0], 1
for j in xrange(2,len(A)):
if A[j]-A[j-1] == A[j-1]-A[j-2]:
opt.append(opt[j-1]+i)
i += 1
else:
opt.append(opt[j-1])
i = 1
def numberOfArithmeticSlices(self, A):
"""
:type A: List[int]
:rtype: int
"""
opt, i = [0,0], 1
for j in xrange(2,len(A)):
if A[j]-A[j-1] == A[j-1]-A[j-2]:
opt.append(opt[j-1]+i)
i += 1
else:
opt.append(opt[j-1])
i = 1
return opt[-1]
求至少3连续元素等差的个数
https://leetcode.com/problems/arithmetic-slices/#/solutions
class Solution(object):
def numberOfArithmeticSlices(self, A):
"""
:type A: List[int]
:rtype: int
"""
cur, sums = 0, 0
for j in xrange(2,len(A)):
if A[j]-A[j-1] == A[j-1]-A[j-2]:
cur += 1
sums +=cur
else:
cur = 0
return sums
阅读全文
0 0
- 413. Arithmetic Slices
- [LeetCode]413.Arithmetic Slices
- Leetcode 413. Arithmetic Slices
- Leetcode-413. Arithmetic Slices
- 413. Arithmetic Slices
- 413. Arithmetic Slices
- 【LeetCode】413. Arithmetic Slices
- 413. Arithmetic Slices
- Leetcode-413. Arithmetic Slices
- 413. Arithmetic Slices
- 413. Arithmetic Slices
- 413. Arithmetic Slices
- 413. Arithmetic Slices
- LeetCode 413. Arithmetic Slices
- 413. Arithmetic Slices 【M】
- 413. Arithmetic Slices
- Leetcode 413. Arithmetic Slices
- 413. Arithmetic Slices
- 动态拼接的click事件不能被触发
- 学习Linux命令(26)
- 微信小程序开发工具快捷键学习
- Ubuntu学习日记--Lesson2:创建、重命名、删除文件及文件夹,强制清空回收站方法
- android listview/ScrollView 去除顶部下拉时的半月形阴影和上下滑动顶部和底部的虚化
- 413. Arithmetic Slices
- textarea 文本框禁止拖动改变大小
- 一个可以自动生成CSS样式前缀的脚本---Prefixfree.js
- java异常详解
- Zookeeper实例ZkClient API-删除节点
- Oracle基本命令
- 排名
- 【css】下拉菜单那些事儿
- 悬镜安全实验室携新产品“云鉴”亮相NSC2017中国网络安全大会