diff --git a/cpp/2203/220303.cpp b/cpp/2203/220303.cpp new file mode 100644 index 0000000..db1f536 --- /dev/null +++ b/cpp/2203/220303.cpp @@ -0,0 +1,40 @@ +#include +#include + +/** + * 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& 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 args = {1,2,3,8,9,10}; + std::cout << Solution::numberOfArithmeticSlices(args); + return 0; +} diff --git a/cpp/2203/CMakeLists.txt b/cpp/2203/CMakeLists.txt index d8a769f..13fa46e 100644 --- a/cpp/2203/CMakeLists.txt +++ b/cpp/2203/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2203) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2203 220302.cpp) +ADD_EXECUTABLE(2203 220303.cpp)