# vue2 data hijacking principle

2022-12-28   ES

Given an integer array. Your task is to find the incremental sub -sequence of all the array. The length of the incremental subsequent sequence is at least 2.

Example:

Input: [4, 6, 7, 7]
output: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7], [6, 7], [6, 7, 7], [ 7,7], [4,7,7]]]

Instructions:

The length of the

1. given no group will not exceed 15.

The integer range in the

2. array is [-100,100].

The

3. given number may contain duplicate numbers, and equal numbers should be regarded as an increasing situation.

Source: LEETCODE

# solution one

The simplest and rude implementation is to use DFS to obtain incremental subsequent sequences, and then use STD :: SET to row the sub -sequence.

code:

``````class solution
{{
public:
Vector <Vector <th >> FindSubsequEnces (Vector <ent> & Nums)
{{
_dfs (nums, 0);
Return {_set.begin (), _set.end ()};
}

Private:
void_dfs (vector <int> & nums, int index)
{{
if (index == nums.size ())
{{
if (_seq.size (>> 1)
_set.insert (_Seq);
Return;
}

// Branch 1: Select NUMS [Index]
if (_seq.empty () || nums [index]> = _Seq.Back ())
{{
_seq.push_back (nums [index]);
_dfs (nums, index + 1);
_Seq.pop_back ();
}

// Branch 2: Do not choose nums [index]
_dfs (nums, index + 1);
}

Private:
set <Vector <int >> _Set;
vector <int> _Seq;
};``````

# 2

In addition to the use of SET, there are also more efficient rowing methods.

The sub -sequence of the sub -sequence through DFS may be duplicate because there may be duplicate elements in the source sequence.

For example, for the source sequence {A1, B1, B2, C, …}, B1 = B2.

Assuming that in a certain iteration, the sub -sequence is listed as {A1, B1}, and the currently traversed element is B2. According to the original solution, there are two branches:

branch 1: B2 enter the sub -sequence, get {A1, B1, B2}, and continue to traverse from C next time.

branch 2: B2 does not enter the sub -sequence, get {A1, B1}, and continue to traverse from C next time.

Among them, branches 2 and “No B1, select B2” path {A1, B2, …} Repeat.

If you avoid this duplicate? It is to do a test at 2 when the branch is performed. If the current element is equal to the tail element of the current subsequent sequence, the branch will not be performed 2.

For example, in the above example, the current element B2 and the current subsequence {A1, B1} are equal, so the branch 2 is not performed.

code:

``````class solution
{{
public:
Vector <Vector <th >> FindSubsequEnces (Vector <ent> & Nums)
{{
_dfs (nums, 0);
Return_ans;
}

Private:
void_dfs (vector <int> & nums, int index)
{{
if (index == nums.size ())
{{
if (_seq.size (>> 1)
_Aans.push_back (_Seq);
Return;
}

// Branch 1: Select NUMS [Index]
if (_seq.empty () || nums [index]> = _Seq.Back ())
{{
_seq.push_back (nums [index]);
_dfs (nums, index + 1);
_Seq.pop_back ();
}

// Branch 2: Do not choose nums [index]
if (_seq.empty () || nums [index]! = _Seq.Back ())
_dfs (nums, index + 1);
}

Private:
Vector <Vector <int >> _ANS;
vector <int> _Seq;
};``````

Welcome to our WeChat public account: CPP note

source