目录

lc446.等差数列划分 II - 子序列

446. 等差数列划分 II - 子序列

暴力枚举所有子序列

  • 枚举子集就是枚举所有的子序列
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
typedef long long LL;
class Solution {
public:
    int ans = 0;
    void dfs(vector<int>& nums, vector<LL>& path, int cur){
        if(path.size() >= 3){
            for(int i = 1; i < path.size(); i++){
                if(path[i] - path[i - 1] != path[1] - path[0]){
                    return;
                }
            }
            ans += 1;
        }

        for(int i = cur; i < nums.size(); i++){
            path.push_back(nums[i]);
            dfs(nums, path, i + 1);
            path.pop_back();
        }
    }
    int numberOfArithmeticSlices(vector<int>& nums) {
        vector<LL> path;

        dfs(nums, path, 0);

        return ans;
    }
};