有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java输出包含一个或多个元素的整数数组

我的要求是:

  • longestStreak:布尔数组->;整数
  • 目的:计算最长的连续条纹的长度
  • 输入参数值中出现的true
  • 输入:值是长度至少为1的非空布尔数组
  • 输出:输出在中找到的最大连续True数
  • 输入数组

这就是我目前所做的:


public class Problem{


  public static int[] longestStreak(boolean[] values) {
    int [] returnValues = new int[1];
    //when streakCounter has a value of 0, we are trying to find a new streak
    int streakCounter = 0, 
      //
      longestStreakFound = 0;

    for(int boolCounter = 0; boolCounter < values.length; boolCounter++){
      //if true increment streakCounter
      if (values [boolCounter]){
        //if starting a new streak, store index to the end of returnValues
        if(streakCounter == 0){
          //method name would be growArrayAndCopyValuesFromOriginal
          int[] tempValues = new int[returnValues.length + 1];

          for(int originalValueCounter = 0; originalValueCounter < returnValues.length; originalValueCounter++ ){
            tempValues[originalValueCounter] = returnValues [originalValueCounter]; 
          }

          //originalValueCounter is not available in this scope
          //System.out.println(originalValueCounter);

          returnValues = tempValues;
          //end growArrayAndCopyValuesFromOriginal method

          returnValues [returnValues.length-1] = boolCounter;
        }

        streakCounter++;
      }
      else{//if false do...
        if (longestStreakFound < streakCounter){
          longestStreakFound = streakCounter;
        }
        streakCounter = 0;
      }
    }

    returnValues[0] = longestStreakFound; 
    return returnValues;
  }

  /**
   * This main method is a test for the longestStreak method.
   * In the future it would be best to place this into a test class.
   */
  public static void main(String[] args){
    boolean[] bools = new boolean[]{true, true, true, false, true, true, true, true, true, false, true}; 


    System.out.print("Longest streak found: " + longestStreak(bools));
  }

}

预期结果:[2,0,3,8]

实际结果:[2,0,3,6,8]

有人能帮我找出我做错了什么吗


共 (1) 个答案

  1. # 1 楼答案

    这是我的解决方案:

    package test;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.TreeMap;
    
    public class LongestStreak {
    
        static class Result {
            int maximumSequenceLength;
            int[] occurrenceIndices;
    
            public String toString() {
                return "max. seq. length: " + maximumSequenceLength + ", at " + Arrays.toString(occurrenceIndices);
            }
        }
    
        public static void main(String[] args) {
            List<Boolean> input = Arrays.asList(
    //              new Boolean[] { true, true, false, false, true, true, true, false, true, false, true, true, true, false }
    //              new Boolean[] { true, true }
    //              new Boolean[] { true, false, true }
                    new Boolean[] { true, false, true, true }
                    );
    
            TreeMap<Integer, Result> resultMap = new TreeMap<>();
    
            boolean last = false;
            Result tmp = null;
    
            for (int i = 0; i < input.size(); i++) {
                boolean actual = input.get(i);
                if (!last && actual) {
                    System.out.println("new sequence starts: " + i);
                    tmp = new Result();
                    tmp.occurrenceIndices = new int[] { i }; 
                }
                if (actual) {
                    System.out.println("new sequence continues: " + i);
                    tmp.maximumSequenceLength++;
                }
                if (!actual && last 
                        //or collection ends
                        || i == input.size() - 1) {
                    System.out.println("new sequence ends: " + i);
                    System.out.println(tmp);
                    Result present = resultMap.get(tmp.maximumSequenceLength);
                    if (present != null) {
                        System.out.println("append occurrence to existing maximum sequence of " + tmp.maximumSequenceLength);
                        int[] dest = new int[present.occurrenceIndices.length + 1]; 
                        dest[present.occurrenceIndices.length] = tmp.occurrenceIndices[0];
                        System.arraycopy(present.occurrenceIndices, 0, dest, 0, present.occurrenceIndices.length);
                        present.occurrenceIndices = dest;
                    } else {
                        System.out.println("new maximum sequence length of " + tmp.maximumSequenceLength);
                        resultMap.put(tmp.maximumSequenceLength, tmp);
                    }
                }
    
                last = actual;
            }
    
            if (resultMap.isEmpty()) {
                System.out.println("collection contains any trues");
            } else {
                System.out.println("Result: " + resultMap.lastEntry().getValue());
            }
        }
    }
    

    循环检查从falsetrue的“上升边”,并将位置保存在tmp对象中,然后开始计算True的数量,并将其存储在tmp对象中

    当“边”从true下降到falsetmp对象存储在resultMap中,出现次数作为键

    如果已经有一个对象与相同数量的引用相关联,只需将引用索引附加到现有对象的数组中即可

    TreeMap按键自动对其内容排序。因此,最大真实数的结果出现在地图的最后一个元素中

    true, false, true, true的输出是:

    new sequence starts: 0
    new sequence continues: 0
    new sequence ends: 1
    max. seq. length: 1, at [0]
    new maximum sequence length of 1
    new sequence starts: 2
    new sequence continues: 2
    new sequence continues: 3
    new sequence ends: 3
    max. seq. length: 2, at [2]
    new maximum sequence length of 2
    Result: max. seq. length: 2, at [2]