add: 220303 [cpp]
This commit is contained in:
parent
55e35c0718
commit
42bd393831
|
|
@ -0,0 +1,40 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 413. Arithmetic Slices
|
||||||
|
* An integer array 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, [1,3,5,7,9], [7,7,7,7], and [3,-1,-5,-9] are arithmetic sequences.
|
||||||
|
* Given an integer array nums, return the number of arithmetic subarrays of nums.
|
||||||
|
* A subarray is a contiguous subsequence of the array.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
static int numberOfArithmeticSlices(std::vector<int>& args) {
|
||||||
|
int n = args.size();
|
||||||
|
if (n < 3)
|
||||||
|
return 0;
|
||||||
|
args.push_back(0xFFFFFFF);
|
||||||
|
int prev = args[1], diff = args[1] - args[0], sliceBegin = 0, ret = 0;
|
||||||
|
for (int i = 2; i <= n; ++i) {
|
||||||
|
if (args[i] - prev != diff) {
|
||||||
|
// Last slice terminates at position i - 1
|
||||||
|
// Length i - sliceBegin
|
||||||
|
int len = i - sliceBegin - 2;
|
||||||
|
if (len > 0)
|
||||||
|
ret += (len * (1 + len)) >> 1;
|
||||||
|
diff = args[i] - prev;
|
||||||
|
sliceBegin = i - 1;
|
||||||
|
}
|
||||||
|
prev = args[i];
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::vector<int> args = {1,2,3,8,9,10};
|
||||||
|
std::cout << Solution::numberOfArithmeticSlices(args);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
@ -3,4 +3,4 @@ PROJECT(2203)
|
||||||
|
|
||||||
SET(CMAKE_CXX_STANDARD 23)
|
SET(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
ADD_EXECUTABLE(2203 220302.cpp)
|
ADD_EXECUTABLE(2203 220303.cpp)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue