March 6th day activities punch face questions 57 questions: a continuous and positive sequence s (simple)
Title: 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.
Problem-solving ideas: the double pointer method, and greater than t, the pointer to the left to the right, is less than t, and the right pointer to the right.
classSolution{publicint[][]findContinuousSequence(int target){
List<int[]> ansList =newArrayList<>();int start =1;int end =0;int sum =0;while(end<=(target+1)/2){if(sum < target){
end++;
sum += end;}elseif(sum > target){
sum-=start;
start++;}elseif(sum == target){int j =0;int[] ans =newint[end-start+1];for(int i=start;i<=end;i++){
ans[j++]= i;}
ansList.add(ans);
end++;
sum += end;}}return ansList.toArray(newint[0][]);}}
Problem solution approach: This approach became a math problem to find the law, if (ti) / (i ++) = 0, there is the i-th element in the array, I do not know how this law is to come. . . But also a fantastic double 100%!
classSolution{publicint[][]findContinuousSequence(int target){
List<int[]> result =newArrayList<>();int i =1;while(target>0){
target -= i++;if(target>0&& target%i ==0){int[] array =newint[i];for(int k = target/i, j =0; k < target/i+i; k++,j++){
array[j]= k;}
result.add(array);}}
Collections.reverse(result);return result.toArray(newint[0][]);}}
作者:VaporMax
链接:https://leetcode-cn.com/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof/solution/java-shuang-100-by-vapormax/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。