我的要求是:
这是我到目前为止所做的:
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]
任何人都可以帮我弄清楚我在做错了吗?
答案 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;从false
到true
并保存tmp
对象中的位置,然后开始计算真实数量并将其存储在tmp
对象中。
当&#34;边缘下降&#34;从true
到false
,tmp
对象存储在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]