Interview questions 57 - II and the number of consecutive positive pointer sequence s double Golang.

Interview questions 57 -. II and the number of consecutive positive sequence s

Enter a positive integer target, output of all consecutive positive integers, and the target 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:

Input: target = 9
Output: [[2,3,4], [4,5]]
Example 2:

Input: target = 15
Output: [[1,2,3,4,5], [4,5,6], [7,8]]

limit:

1 <= target <= 10^5

My thoughts

This question can also use arithmetic sequence summation, but that is a mathematical problem, and programming skills does not matter, here is the two-pointer, easy to understand.
Was added to the i j, and smaller on the right pointer slide, and slide on the large left pointer, just a set of outputs then slide left pointer.

My answer

func findContinuousSequence(t int) (ans [][]int) {
	i,j:=1,2
	for ;i<=t/2;i++{
		for ;;j++{
			if (i+j)*(j-i+1)>2*t{
				break
			}else if (i+j)*(j-i+1)==2*t{
				ans=append(ans,createSlice(i,j))
				break
			}
		}
	}
	return
}
func createSlice(i,j int) (ans []int) {
	for n:=i;n<=j;n++{
		ans=append(ans,n )
	}
	return
}
Published 38 original articles · won praise 0 · Views 1037

Guess you like

Origin blog.csdn.net/Cyan1956/article/details/104691498