[Leetcode] 413. Arithmetic Slices
来源:互联网 发布:中老年人学英语软件 编辑:程序博客网 时间:2024/05/01 13:15
题目:leetcode链接
题意:求一串数字中有3个及以上连续数字组成的等差数列的个数。
例子:
[1,2,3,4] 组成的有3个分别是[1,2,3],[2,3,4],[1,2,3,4];
解题思路:
求出该数组中能有超过连续3个数组成等差数列的个数,然后再把每个等差数列进行拆分,求最终的个数。
具体代码实现:
public class Solution { public int numberOfArithmeticSlices(int[] A) { int result = 0; int len = A.length; if (len < 3) { return 0; } List<Integer> list = new ArrayList<Integer>(); int interval = A[1] - A[0]; int flag = 2; for (int i = 2; i < len; i++) { int temp = A[i] - A[i - 1]; if (temp == interval) { flag++; if(i == len - 1){ list.add(flag); } } else { if (flag > 2) { list.add(flag); } flag = 2; interval = temp; } } int list_len = list.size(); for (int i = 0; i < list_len; i++) { int t = (list.get(i) - 2) * (list.get(i) - 2 + 1)/2; result += t; } return result; }}
0 0
- [LeetCode]413.Arithmetic Slices
- Leetcode 413. Arithmetic Slices
- Leetcode-413. Arithmetic Slices
- 【LeetCode】413. Arithmetic Slices
- Leetcode-413. Arithmetic Slices
- LeetCode 413. Arithmetic Slices
- Leetcode 413. Arithmetic Slices
- LeetCode 413. Arithmetic Slices
- [LeetCode]413. Arithmetic Slices
- LeetCode 413. Arithmetic Slices
- 【LeetCode】 413. Arithmetic Slices
- [LeetCode]413. Arithmetic Slices
- [leetcode]413. Arithmetic Slices
- [Leetcode] 413. Arithmetic Slices
- LeetCode 413. Arithmetic Slices
- LeetCode 413. Arithmetic Slices
- [leetcode]--413. Arithmetic Slices
- 【LeetCode】413. Arithmetic Slices
- 【游戏设计模式】之一 序言:架构,性能与游戏
- KCF 跟踪算法(3)
- 什么是FPS游戏
- 各个大神博客地址
- react native组件通信以及
- [Leetcode] 413. Arithmetic Slices
- UVA 10600 ACM Contest and Blackout(最小生成树and次小生成树)
- python学习记录--set
- css学习笔记
- androidstudio视频录制和截图功能
- Tomcat启动脚本
- linux tar 命令
- elicpse中布局编写时出现Element <RelativeLayout> is unknown.
- ZCMU-1382-数位dp