几乎增加了序列java

时间:2018-05-20 00:24:21

标签: java arrays for-loop

我正在尝试编写代码,通过从该数组中仅删除一个元素来确定是否可以获得严格增加的整数数组。

我的代码适用于17个案例中的16个,但是无法想出一种方法来巧妙地重写我的代码,以便它解决一个数字大于前面的数字的情况以及小于之后的数字。它是我写这个循环的方式。这是我的代码。这不起作用的情况是数组:[1,2,3,4,3,6],因为它不会将数组中的最后3个视为违规者,因为我的for循环当前是被构造的

boolean almostIncreasingSequence(int[] sequence) {

int offenderPosition = 0;
int[] arrCopy = Arrays.copyOf(sequence, sequence.length);
boolean ordered = true;


//trying to neatly rewrite this for loop 
for(int i= 0; i < sequence.length; i++){
    if(i<sequence.length-1){
        for(int j = i+1; j < sequence.length; j++) {
            if(!(sequence[i] < sequence[j])){
                ordered = false;
                offenderPosition = i;
            }
        }
    }
    if(i == sequence.length-1){
        if(!(sequence[i] > sequence[i-1])){
            ordered = false;
            offenderPosition = i; 
        }
    }

}


if(ordered == false) {
    //remove offender 
    int currentSize = arrCopy.length;
    for(int i = offenderPosition+1;i< currentSize; i++) {
        arrCopy[i-1] = arrCopy[i];
    }
    currentSize--;

    //reassign array
    arrCopy = Arrays.copyOf(arrCopy, currentSize);

    boolean lastChance = true;

    for(int i = 0; i < currentSize-1; i++){
        for(int j = i+1; j < currentSize; j++) {
            if(!(arrCopy[i] < arrCopy[j])){
                lastChance = false;
            }
        }
    }
    return lastChance;
}
else{
    return true;
}

}

4 个答案:

答案 0 :(得分:0)

您可以将代码分解为几种方法:

// The first method just checks if the input array is sorted
public static boolean isAscending(int[] arr) {

    boolean sorted = true;
    for (int i = 0; i < arr.length - 1; ++i) {
        if (arr[i] >= arr[i + 1]) {
            sorted = false;
            break;
        }
    }
    return sorted;
}

// The second method is the important one.
public static boolean isAlmostAscending(int[] array) {
    int[] tmpArray = new int[array.length - 1];
    // loop through all possible combinations
    for(int i = 0; i < array.length; ++i) {
        copyArray(array, tmpArray, i);
        if(isAscending(tmpArray)) {
            // if the array is sorted after skipping element i, we are done
            return true;
        }
    }
    return false;
}

// helper method to copy array and skip element at skip
private static void copyArray(int[] srcArray, int[] destArray, int skip) {
    for(int i = 0, j = 0; i < destArray.length; ++i, ++j) {
        if(i == skip) {
            ++j;
        }
        destArray[i] = srcArray[j];
    }
}

您可以将所有三种方法合并为一个,如下所示:

public static boolean isAlmostAscending(int[] array) {
    int[] tmpArray = new int[array.length - 1];
    // loop through all possible combinations
    for(int index = 0; index < array.length; ++index) {

        // copyArray
        for(int i = 0, j = 0; i < tmpArray.length; ++i, ++j) {
            if(i == index) {
                ++j;
            }
            tmpArray[i] = array[j];
        }

        // check if the current array is sorted
        boolean sorted = true;
        for (int i = 0; i < tmpArray.length - 1; ++i) {
            if (tmpArray[i] >= tmpArray[i + 1]) {
                sorted = false;
                break;
            }
        }
        if(sorted) {
            // if the array is sorted after skipping element i, we are done
            return true;
        }
    }
    return false;
}

答案 1 :(得分:0)

我认为这可能有效:

boolean almostIncreasingSequence(int[] a) {
    int count1 = 0 , count2 = 0;
    for(int i = 0 ; i < a.length-1 ; i++){
        if(a[i] >= a[i+1]) count1++;
    }

    for(int i = 0 ; i < a.length-2 ; i++){
        if(a[i] >= a[i+2]) count2++;
    }
     return (count1 <=1) && (count2 <= 1);
}

第一个循环仅检查彼此靠近的数字。如果第一个索引大于第二个索引,我们将为count1加1。当将1加到count1时,意味着第一个索引大于第二个索引,该方法应返回false;否则,方法将返回false。第二个for循环还将检查ex。如果第一个索引大于第三个索引。例如1、2、1,2,它将为count2加1。每次执行循环时,该方法将返回if语句返回的布尔值。

答案 2 :(得分:0)

这是解决方案:

{{1}}

答案 3 :(得分:0)

boolean almostIncreasingSequence(int[] sequence) {
    int count=0;
    for(int i=0;i<sequence.length-1;i++){
        //if a problem is found increase the counter
        if(sequence[i]>=sequence[i+1]){
            count++;
            /*Lots of conditions, just making sure we don't go out of bounds so
              that we can check if there are any potential duplicates/order issues if 
            a number would be removed
            */
            if(i-1>=0&&i+2<sequence.length&&sequence[i- 
            1]>=sequence[i+1]&&sequence[i]>=sequence[i+2]){
                return false;
            }
            //if we found a problem twice
            if(count>=2){
                return false;
            }
        }
    } return true;
}