March 6th day activities punch face questions 57 questions: a continuous and positive sequence s (simple)

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.
    Here Insert Picture Description
  • 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.
class Solution {
    public int[][] findContinuousSequence(int target) {
        List<int[]> ansList = new ArrayList<>();
        int start = 1;
        int end = 0;
        int sum = 0;
        while(end<=(target+1)/2){
            if(sum < target){
                end++;
                sum += end;
            }else if(sum > target){
                sum-=start;
                start++;
            }else if(sum == target){
                int j = 0;
                int[] ans = new int[end-start+1];
                for(int i=start;i<=end;i++){
                    ans[j++]= i;
                }
                ansList.add(ans);
                end++;
                sum += end;
            }
        }
        return ansList.toArray(new int[0][]);
        
    }
}

Here Insert Picture Description

  • 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%!
class Solution {
    public int[][] findContinuousSequence(int target) {
        
        List<int[]> result = new ArrayList<>();
        int i = 1;

        while(target>0)
        {
            target -= i++;
            if(target>0 && target%i == 0)
            {
                int[] array = new int[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(new int[0][]);       
    }
}

作者:VaporMax
链接:https://leetcode-cn.com/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof/solution/java-shuang-100-by-vapormax/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Here Insert Picture Description

Published 100 original articles · won praise 12 · views 2362

Guess you like

Origin blog.csdn.net/new_whiter/article/details/104694261