> For the complete documentation index, see [llms.txt](https://zhongquan789.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zhongquan789.gitbook.io/leetcode/lcof/mian-shi-ti-57-ii.md).

# 面试题57 - II

输入一个正整数 target ，输出所有和为 target 的连续正整数序列（至少含有两个数）。

序列内的数字由小到大排列，不同序列按照首个数字从小到大排列。

```
示例 1：

输入：target = 9
输出：[[2,3,4],[4,5]]

示例 2：

输入：target = 15
输出：[[1,2,3,4,5],[4,5,6],[7,8]]
```

## 限制：

* 1 <= target <= 10^5

## Solutions

1. **math**
2. Similar to `problem 829`. Check it for detailed explanation.

```cpp
class Solution {
public:
    vector<vector<int>> findContinuousSequence(int target) {
        target--;
        int num = 2;
        vector<vector<int>> res;
        while (target >= num) {
            if (target % num == 0) {
                vector<int> arr(num, target / num);
                for (int i = 0; i < num; i++)
                    arr[i] += i;
                res.push_back(move(arr));
            }
            target -= num;
            num++;
        }

        reverse(res.begin(), res.end());
        return res;
    }
};
```

Or

```cpp
class Solution {
public:
    vector<vector<int>> findContinuousSequence(int target) {
        int num = 2, n = 2 * target;

        vector<vector<int>> res;
        while (num * num < n) {
            if (n % num == 0 && (n / num - num) % 2 == 1) {
                vector<int> arr(num, (n / num - num + 1) / 2);
                for (int i = 0; i < num; i++)
                    arr[i] += i;
                res.push_back(move(arr));
            }
            num++;
        }

        reverse(res.begin(), res.end());
        return res;
    }
};
```
