current position:Home>Java judges whether there are 30 consecutive int arrays with a length of 1000 as the growth trend

Java judges whether there are 30 consecutive int arrays with a length of 1000 as the growth trend

2022-02-03 00:49:32 CSDN Q & A

I almost know the train of thought , But I don't know how to turn it into code .
Cycle this 1000 A digital , If arr[i+1]>arr[i] Just save this number in a new array , This condition is not met , This array is empty , Go on , The new length in the array =30 Just take it out , How to write the code ?




Refer to the answer 1:

The sample code is as follows .

    private static boolean exists(int[] numbers) {        int count = 0;        for (int i = 1; i < numbers.length; i++) {            if (numbers[i - 1] < numbers[i]) {                count++;                if (count == 30) {                    break;                }            } else {                count = 0;            }        }        return count == 30;    }



Refer to the answer 2:



Refer to the answer 3:

It's easier to use sliding windows

    public int[] fun(int a[]){        int i = 0,j = 0;        while(true){            if(j == 1000) break;            if(j - i + 1 == 30){                return Arrays.copyOfRange(a, i, j + 1);            }            if(a[i] >= a[j]){                i = j;                j = i + 1;            }else{                j ++;            }        }        return new int[]{};    }



Refer to the answer 4:



Refer to the answer 5:
public class Test extends BaseSolution {    /** *  Check  int Array , Is there a continuous  needUpLen  One is the growth trend  * @param val  data  * @param needUpLen  Continuous growth length  * @return  With the return  true */    private boolean check(int[] val, int needUpLen) {        if (val.length == 0) {            return false;        }        int left = 0, right = 1;        while (right < val.length) {            if (val[right] > val[right - 1]) {                right++;                if (right - left >= needUpLen) {                    //  The output is continuous  needUpLen  The increasing part of the length                     printVals(Arrays.copyOfRange(val, left, right));                    return true;                }            } else {                left = right++;            }        }        return false;    }    public static void main(String[] args) {        Test solution = new Test();        int[] val = new int[] {                9, 84, 85, 47, 29, 35, 25, 29, 93, 57, 94, 18, 31, 12, 3, 96, 70, 1, 87, 1, 3, 43, 77, 50, 84                , 8, 45, 64, 50, 63, 60, 2, 61, 30, 6, 53, 59, 15, 37, 46, 70, 98, 57, 22, 49, 87, 11, 84, 89, 12, 83, 43, 99, 41, 26        };        printVals(val);        System.out.println(solution.check(val, 4));    }}

This is the sliding window, okay ? The one upstairs is still n Square complexity ? Can't copy




Refer to the answer 6:



Refer to the answer 7:
private int[] fun(int[] arr){    int[] result = null;    int count = 0;    for(int i = 0; i < arr.length - 1; i++){        if(arr[i] < arr[i+1]){            if(result == null){                result = new int[30];                result[count] = arr[i];            }            result[++count] = arr[i+1];            if(count == 29){                //  from 0 Start to 29, Length is 30  了                 return result;            }        }else{            result = null;            count = 0;        }    }     if(count < 29){        result = null;    }    return result;}



Refer to the answer 8:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030049300069.html

Random recommended