【问题标题】:almost increasing sequence java几乎递增的序列java
【发布时间】:2018-10-30 00:51:32
【问题描述】:

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

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

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;
}

}

【问题讨论】:

    标签: java arrays for-loop


    【解决方案1】:

    我认为这可能有效:

    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;第二个 for 循环也将检查 ex。如果第一个索引大于第三个索引。 1, 2 ,1,2 例如,它会将 1 添加到 count2 每个循环执行后,该方法将返回 if 语句返回的布尔值。

    【讨论】:

    • 你能解释一下你做了什么吗?
    • 第一个循环只检查彼此接近的数字。如果第一个索引大于第二个索引,我们将在 count1 中加 1。当计数加 1 时,表示第一个索引大于第二个索引,该方法应返回 false;第二个 for 循环也将检查 ex。如果第一个索引大于第三个索引。 1, 2 ,1,2 例如,它会将 1 添加到 count2 执行每个循环后,该方法将返回 if 语句返回的布尔值。
    • 很棒的解决方案,只需 O(n)
    【解决方案2】:

    你可以把代码分解成几个方法:

    // 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;
    }
    

    【讨论】:

    • 说,假设分配给我的编码站点希望通过一种方法完成此操作。我如何调整我的代码来完成所有可能情况下的任务?
    • @ganele892 我明白了,这个过程,或者至少我看到的方式,将是相似的:复制除 currentOffernder 之外的所有内容,检查是否排序,如果为 true,则返回,如果不是则继续。我已经编辑了我的答案,将所有内容集中到一个方法中。
    【解决方案3】:

    解决办法如下:

      boolean almostIncreasingSequence(int[] sequence) {
           int count = checkSeq(sequence);
         if(count == -1) {
             return true;
         }
         int index = count;
         count = checkSeq(removeIndex(sequence,count-1));
        if(count == -1) {
             return true;
         }
         count = checkSeq(removeIndex(sequence,index));
    if(count == -1) {
             return true;
         }
    return false;
    }
    int checkSeq(int[] source){
        int index = -1;
         for(int i=1; i<source.length;i++){
               if(source[i] <= source[i-1]){
                 index = i;
                 break;
             }
         }
    
        return index;
    }
    
    int[] removeIndex(int[] source , int index){
        int[] dist = new int[source.length-1];
        int x = 0;
        for(int i=0; i<source.length;i++){
             if(i==index){
                continue;
            }
            dist[x++] = source[i];
        }
        return dist;
    }
    

    【讨论】:

      【解决方案4】:
      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;
      }
      

      【讨论】:

      • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助,质量更高,更有可能吸引投票。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-13
      • 2017-07-14
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      相关资源
      最近更新 更多