摘要:题目要求将包含大于等于三个元素且任意相邻两个元素之间的差相等的数组成为等差数列。现在输入一个随机数组,问该数组中一共可以找出多少组等差数列。
题目要求
A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. For example, these are arithmetic sequence: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 The following sequence is not arithmetic. 1, 1, 2, 5, 7 A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 <= P < Q < N. A slice (P, Q) of array A is called arithmetic if the sequence: A[P], A[p + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q. The function should return the number of arithmetic slices in the array A. Example: A = [1, 2, 3, 4] return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.
将包含大于等于三个元素且任意相邻两个元素之间的差相等的数组成为等差数列。现在输入一个随机数组,问该数组中一共可以找出多少组等差数列。
思路一:动态规划假设已经知道以第i-1个数字为结尾有k个等差数列,且第i个元素与i-1号元素和i-2号元素构成了等差数列,则第i个数字为结尾的等差数列个数为k+1。因此我们可以自底向上动态规划,记录每一位作为结尾的等差数列的个数,并最终得出整个数列中等差数列的个数。代码如下:
public int numberOfArithmeticSlices(int[] A) { int[] dp = new int[A.length]; int count = 0; for(int i = 2 ; i思路二:算数方法 首先看一个简单的等差数列1 2 3, 可知该数列中一共有1个等差数列
再看1 2 3 4, 可知该数列中一共有3个等差数列,其中以3为结尾的1个,以4为结尾的2个
再看1 2 3 4 5, 可知该数列中一共有6个等差数列,其中以3为结尾的1个,4为结尾的2个,5为结尾的3个。综上,我们可以得出,如果是一个最大长度为n的等差数列,则该等差数列中一共包含的等差数列个数为(n-2+1)*(n-2)/2,即(n-1)*(n-2)/2。
因此,我们只需要找到以当前起点为开始的最长的等差数列,计算该等差数列的长度并根据其长度得出其共包含多少个子等差数列。
代码如下:
public int numberOfArithmeticSlices2(int[] A) { if(A.length <3) return 0; int diff = A[1]-A[0]; int left = 0; int right = 2; int count = 0; while(right < A.length) { if(A[right] - A[right-1] != diff) { count += (right-left-1) * (right-left-2) / 2; diff = A[right] - A[right-1]; left = right-1; } right++; } count += (right-left-1) * (right-left-2) / 2; return count; }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/74259.html
摘要:复杂度思路找数组里面的等差数列的个数。想法是如果一开始三个数就满足是等差数列的话,就在当前已有的数目上不断的累加的结果。 Leetcode[413] Arithmetic Slices A sequence of number is called arithmetic if it consists of at least three elements and if the diffe...
摘要:这个题没什么好说的,用栈就可以了,注意一下两个数计算的时候谁前谁后就行了。 Evaluate Reverse Polish Notation https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ Evaluate the value of an arithmetic expression in Reve...
Problem Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Note: Division between two inte...
摘要:栈法复杂度时间空间思路逆波兰表达式的计算十分方便,对于运算符,其运算的两个数就是这个运算符前面的两个数。注意对于减法,先弹出的是减号后面的数。 Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operato...
摘要:我们一般看到的数学表达式就是中缀表达式,也就是将符号放在两个数字之间。后缀表达式也就是将运算符放在相应数字的后面。后缀表达式相当于树中的后序遍历。通过获得对应位置的操作符。如果对应的还是操作符,则继续递归往前计算。 题目要求 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid...
阅读 870·2021-09-02 09:55
阅读 1495·2019-12-27 12:02
阅读 1684·2019-08-30 14:24
阅读 1138·2019-08-30 14:18
阅读 2750·2019-08-29 13:57
阅读 2193·2019-08-26 11:51
阅读 1364·2019-08-26 10:37
阅读 763·2019-08-23 16:09