当前位置:网站首页>力扣413-等差数列划分——动态规划
力扣413-等差数列划分——动态规划
2022-07-17 17:15:00 【张怼怼√】
题目描述
如果一个数列 至少有三个元素 ,并且任意两个相邻元素之差相同,则称该数列为等差数列。
例如,[1,3,5,7,9]、[7,7,7,7] 和 [3,-1,-5,-9] 都是等差数列。
给你一个整数数组 nums ,返回数组 nums 中所有为等差数组的子数组个数。
子数组 是数组中的一个连续序列。
解题思路
看这道题目的时候也是没有任何思路,甚至读错了题目:要求子数组是数组中连续的序列,所以(1,3,5)并不是(1,2,3,4,5)的子数组。建立一个数组 arr 用于存放以每个元素结尾的子数组,因为子数组必须满足三个元素,所以从数组的第三个(索引为2)的元素开始搜索。
假设数组为 (1,2,3,4,5,6,7)
- 3 ---> arr[ 2 ] = 1; (1,2,3)
- 4 ---> arr[ 3 ] = 2; (1,2,3,4); (2,3,4)
- 5 ---> arr[ 4 ] = 3; (1,2,3,4,5); (2,3,4,5); (3,4,5)
- 6 ---> arr[ 5 ] = 4; (1,2,3,4,5,6); (2,3,4,5,6); (3,4,5,6); (4,5,6)
- 7 ---> arr[ 6 ] = 5; (1,2,3,4,5,6,7); (2,3,4,5,6,7); (3,4,5,6,7); (4,5,6,7); (5,6,7)
发现以每一个元素结尾找到的子数组的个数恰好比上一个元素结尾的子数组个数多一个,所以数组(1,2,3,4,5,6,7)的子数组个数就是将每个元素结尾的子数组个数找出来作求和。故这个数组的子数组个数为:1+2+3+4+5=15
所以状态转移方程就很明确了
arr [ i ] = arr [ i -1 ] +1
然后将 arr 数组求和即可。
输入输出示例

代码
class Solution {
public int numberOfArithmeticSlices(int[] nums) {
int len = nums.length;
int[] arr = new int[len];
for(int i = 2; i < len; i++){
if(nums[i] - nums[i - 1] == nums[i - 1] - nums[i - 2]){
arr[i] = arr[i - 1] + 1;
}
}
int sum = 0;
for(int cnt :arr){
sum += cnt;
}
return sum;
}
}边栏推荐
- 数组去重 数组排序 最大值 求和 类数组转化 过滤数组
- Azkaban 安装文档
- 逻辑运算符1(阁瑞钛伦特软件-九耶实训)
- 收益风险比:投资机会最重要指标 2020-03-14
- Structure memory alignment, bit segment, union
- Lanthanide metal organic skeleton( [email protected] )|Rhodamine 6G modified MOF material | catalase @zif composite | MOF
- LeetCode_前缀和_中等_523.连续的子数组和
- Detailed explanation of RAID disk array, raid classification, advantages and disadvantages
- Qiyue supplies cumof nanocrystals loaded with methylene blue | femof nanosheets grown in situ on foam nickel | oxide nanowires /zif MOFs sugar gourd like Composites
- Review the 2008 financial crisis and become a long-term investor 2020-03-19
猜你喜欢

Ultrasonic sensor (chx01) learning notes Ⅲ - I2C reading and writing operation

Ultrasonic sensor (ch101 & ch201) - I

OpenCV:06形态学

Dynamic memory planning

云犀&腾讯云达成战略合作,加速拓展全球直播市场

Database daily question --- day 25: bank account summary II
Do you still need to release the database connection manually with typeorm

Structure memory alignment, bit segment, union

Visual ETL tool kettle concept, installation and practical cases
[email protected] | Porphyrin encapsulated organometallic frame materials [email protected] |
随机推荐
Redis逻辑集群创建
R language -- principle of Cox model calibration curve (I) data source
通货收缩的市场何时反转?我们该如何操作?2020-03-13
竞赛笔记:Numpy学习笔记
Acwing786. 第k个数
ATT&CK实战系列——红队实战(—)
Cadmium sulfide supported mil-125 (TI) | streptavidin (SA) - zirconium porphyrin MOF composite (PCN- [email protected] )|Shell core
音频常见端子剖析图---再也不会搞错了
运维小白成长记—架构第6周
Stable super odds, 9 years old mixin | 2022 Jincang innovative product launch was successfully held
O & M LITTLE WHITE Growth record - architecture week 6
市场“不确定性”中的投资逻辑 2020-03-18
学习记录:调用TFTLCD液晶屏
useEffect 总结
Can you view MySQL data table structure in two ways?
Dynamic memory planning
可视化ETL工具Kettle概念、安装及实战案例
Summary of ultrasonic sensor series articles
Detailed explanation of RAID disk array, raid classification, advantages and disadvantages
[C language programming 7] BTB model