输出带有一个或多个元素

时间:2017-09-29 22:24:05

标签: java arrays

我的要求是:

  • longestStreak:布尔数组 - >整数
  • 目的:计算连续最长条纹的长度
  • 输入参数值中的真实值
  • 输入:值是一个非空的布尔数组,长度至少为1
  • 输出:输出
  • 中找到的最大连续休眠数
  • 输入数组

这是我到目前为止所做的:

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 个答案:

答案 0 :(得分: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());
        }
    }
}

循环检查&#34;上升沿&#34;从falsetrue并保存tmp对象中的位置,然后开始计算真实数量并将其存储在tmp对象中。

当&#34;边缘下降&#34;从truefalsetmp对象存储在resultMap中,其中出现次数为关键。

如果已经存在与相同出现次数关联的对象,则将出现索引附加到现有对象的数组中。

TreeMap按键自动对其内容进行排序。因此,最大数量的trues的结果位于地图的最后一个元素中。

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]