problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::arithmetic_slices::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static int numberOfArithmeticSlices (vector< int > &nums)
 

详细描述

在文件 leetcode.h2466 行定义.

成员函数说明

◆ numberOfArithmeticSlices()

int leetcode::arithmetic_slices::Solution::numberOfArithmeticSlices ( vector< int > &  nums)
static

在文件 leetcode.cpp6632 行定义.

6632 {
6633 const int n = nums.size();
6634 vector<int> diff(n - 1);
6635 for(int i = 0; i < n - 1; i++) {
6636 diff[i] = nums[i + 1] - nums[i];
6637 }
6638 vector<int> consecutive;
6639 int prev = 0;
6640 int cnt = 0;
6641 for(int i = 0; i < n - 1; i++) {
6642 if(diff[i] == diff[prev]) {
6643 cnt++;
6644 } else {
6645 consecutive.emplace_back(cnt);
6646 prev = i;
6647 cnt = 1;
6648 }
6649 }
6650 consecutive.emplace_back(cnt);
6651 int ans = 0;
6652 for(const auto num: consecutive) {
6653 if(num >= 2) {
6654 ans += (num - 1) * num / 2;
6655 }
6656 }
6657 return ans;
6658 }

被这些函数引用 leetcode::arithmetic_slices::TEST().


该类的文档由以下文件生成: