Offer prove safety questions surface 57 -. II and the number of consecutive positive sequence s

Foreword

This series of articles to "prove safety Offer" brush title notes.

Topic Location: force detained Chinese

topic

Enter a positive integer target, and for the output of all targetconsecutive positive integers sequence (containing at least two numbers).

The numbers in ascending sequence are arranged in ascending order of different sequences according to the first digit.

Example 1:

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

Example 2:

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

limit:

1 <= target <= 10^5

Thinking

A method, arithmetic sequence

Because the output of the arithmetic sequence with a tolerance of 1

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

Carefully observe the output is easy to see:

  1. Output is normally provided by two, three, four constitution, are more and more, so the loop from the beginning to find the number of columns 2 and gradually increase the number of bits, so each loop length is the number of columnsn
  2. The results of each subject requirements all add up to an output target, obtained by the summation formula
    arithmetical series summation formula (首项+末项) * n /2
    is converted to:(a1+(a1+n-1))*n/2
  3. Summation formula needs first term and last term, is the corresponding a1and(a1+n-1)
  4. target/nThe median can be obtained, a1and the median distance isn/2

Therefore, the median of an odd number -n/2, an even number is the median number of small, rounded down automatically cut after n/2the value obtained by the multi-reduction1

For example: targetas 9he length of n 3number of sub-columns [2,3,4], median 3, a1that is 2, a1the distance that the median n/2is 3-3/2=2; he length n is 2the number of sub column [4,5], median 4, a1is 4-2/2+1.

Get the logical

if n%2 == 1{
    a1 := target/n - n/2
}else{
	a1 := target/n - n/2 + 1
}

Simplifies to

a1 := target/n - n/2 - (n%2 - 1)

Code

go:

func findContinuousSequence(target int) [][]int {
	var res [][]int
	for n := 2; n < target; n++ {
		a1 := target/n - n/2 - (n%2 - 1)
		if a1 < 1 {
			break
		}
		if target == (a1+(a1+n-1))*n/2  {
			var res_col []int
			for i := a1; i < a1+n; i++ {
				res_col = append(res_col, i)
			}
			res = append(res, res_col)
		}
	}
	var revort_res [][]int
	for i:=len(res)-1;i>=0;i--{
		revort_res = append(revort_res,res[i])
	}
	return revort_res
}

The second method violence enumeration

从1开始暴力枚举,初始子序列为[1,2],求和为3,对比和target的距离,如果小于就继续增加序列长度为[1,2,3]直到求和等于target则把序列加入最终结果集国;如果大于则改变序列起始数字为[2,3]继续寻找。

js:

 var findContinuousSequence = function(target) {
          let arr = [];
          for (let i = 1; i < target - 1; i++) {
            let list = [i, i + 1];

            while (add(list) <= target) {
              if (add(list) === target) {
                arr.push(list);
              } else {
                list.push(list[list.length - 1] + 1);
              }
            }
          }
          console.log(arr);
          return arr;
        };

        function add(arr) {
          const reducer = (total, current) => total + current;
          return arr.reduce(reducer);
        }
发布了131 篇原创文章 · 获赞 15 · 访问量 6万+

Guess you like

Origin blog.csdn.net/BTnode/article/details/104700536