Offer] [prove safety and continuous output of all positive numbers and sequence S is a continuous sequence s is a positive number. The ascending order in the ascending sequence between the sequence numbers in accordance with the start ascending order

Title repeat

Xiao Ming is very fond of mathematics, one day when he was doing math homework, required to calculate and 9 to 16, he immediately wrote the correct answer is 100. But he was not satisfied with this, he wondered how many kinds of continuous positive number sequence is 100 (including at least two numbers). Before long, he got another set of consecutive positive number and a sequence of 100: 18,19,20,21,22. Now the question to you, you can also quickly identify all positive and continuous sequence S? Good Luck!

Output Description:

All positive output and a continuous number sequence S. The ascending order in the ascending sequence between the sequence numbers in accordance with the start ascending order

My idea with the code:

Perhaps the most original idea, but I did after the time complexity of space optimization ideas on offer to prove safety and feel very different complexity, high complexity in the S I still do not add up with the cycle and res.push_back. Welcome to give comment.

Ideas:

First, we understand that such number is 100, then the contiguous sequence 100/2 = certainly no more than 50; 99 and then successively adds such as a digital sequence does not exceed 50 = 99/2 + 1. Here we do not distinguish between odd and even order, and that both the number of consecutive positive sequence S is no more than S / 2 + 1, assuming constant sequence and are tmp;
tmp> ++ when the sum to I;
tmp <to the sum when j ++

Eg. 5 = S
[2. 1. 4. 3. 5]

Equal to the maximum sum of 5 consecutive sequence is no more than 5/2 + 1 = 3; down cycle begins
when tmp <time 5, res will push_back i and J; if tmp> during 5, res empties; when tmp == 5, result.push_back (res), and the process of emptying and res break.

i=1
tmp=i=1<5 那么 tmp=tmp+j=1+2=3<5 那么tmp=tmp+j=1+2+3=6>5 break;

2 = I
tmp = 2 <. 5 then tmp = 2 + 3 = 5 result.push_back (res), and the process of emptying and res break;

. 3 = I
tmp = I =. 3 but j = i + 1 = 4> (5/2 + 1) and the loop exits, return result.

Code

class Solution {
public:
    vector<vector<int> > FindContinuousSequence(int sum) {
        int len = sum / 2 +1;
		vector<vector<int> > result;
		vector<int>res;
		if (sum <3) {
			return result;
		}
		else{
            for (int i = 1; i <= len; i++) {
                int tmp = i;
                res.push_back(i);
                for (int j = i+1; j <= len; j++) {
                    if(tmp<sum){
                        tmp += j;
                        res.push_back(j);
                    }
                    if (tmp > sum) {
                        res.clear();
                        break;
                    }
                    if (tmp ==sum){
                        result.push_back(res);
                        res.clear();
                        break;
                    }
                }
            }
		}    
		return result;
    }
};

Offer ideas to prove safety

Double pointer problem
when the sum is less than the sum, large Continue ++ pointer
or small pointer ++

Link: https: //www.nowcoder.com/questionTerminal/c451a3fd84b64cb19485dad758a55ebe f = discussion?
Source: Cattle-off network

class Solution {
	public:
		vector<vector<int> > FindContinuousSequence(int sum) {
			vector<vector<int> > result;
			vector<int> res;
			int phigh = 2, plow = 1;
			while (phigh > plow) {
				int cur = (phigh + plow) * (phigh - plow + 1) / 2;//等差数列求和
				if (cur < sum)
					phigh++;

				if (cur == sum) {
					for (int i = plow; i <= phigh; i++)
						res.push_back(i);
					result.push_back(res);
					res.clear();
					plow++;
				}

				if (cur > sum)
					plow++;
			}

			return result;
		}
	};
Published 57 original articles · won praise 28 · views 4115

Guess you like

Origin blog.csdn.net/weixin_41747893/article/details/104706402