DP-----413. Arithmetic Slices
来源:互联网 发布:tensorflow lite官网 编辑:程序博客网 时间:2024/06/06 02:07
原题目
暂且称为dp吧,实际上是一点dp的影子都没有
法一:简单说就是找出序列中的所有等差数列,但是注意的是要是最少元素为3的序列同时还不一定是连续的。有可能前半部分和后半部分的差不同。
public int numberOfArithmeticSlices(int []nums){ int result=0; int temp=0; for(int i=1;i<nums.length-1;i++){ if(nums[i]-nums[i-1]==nums[i+1]-nums[i])){ temp++; }else { result+=(temp*(temp-1)/2); temp=0; } } //假如一直持续到末尾的时候根本就没有else的情景 result+=(temp*(temp-1)/2); return result;}
法二 掏出重要的一击,假装是dp的样子
public int numberOfArithmeticSlices(int[] A) { int sum = 0; int tmp = 0; int n = A.length; int []dp=new int[n]; for(int i=1 ; i < n - 1 ;i ++ ){ if( A[i-1]-A[i] == A[i]-A[i+1] ){ dp[i]=dp[i-1]+1; } sum+=dp[i]; } return sum; }
如何寻找的等差数列
阅读全文
0 0
- [DP]413. Arithmetic Slices
- DP-----413. Arithmetic Slices
- LeetCode 413. Arithmetic Slices dp方法
- DP——Arithmetic Slices
- 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
- UBUNTU ifconfig只有lo
- 二维码扫描
- Android 系统重启原因分析
- Bootstrap学习笔记(五)
- 从原理到代码:大牛教你如何用 TensorFlow 亲手搭建一套图像识别模块
- DP-----413. Arithmetic Slices
- MOOC清华《程序设计基础》第6章:分鱼问题(不用数组递推)
- 一张图总结Google C++编程规范(Google C++ Style Guide)
- 常见的提高高并发下访问的效率的手段
- HashMap死循环记录
- c++动态内存管理相关知识
- 实习日志-2
- LEAST() GREATEST()
- 最大流学习hdu3549 && 4920二分图匹配