java - 从列表中获取最长的连续记录

标签 java list

在 Java 中,我正在编写代码来分析另一个程序输出中最长的颜色条纹。该程序可以输出三种颜色:红色、绿色和黑色。绿色算作正在检查的颜色(例如 RRGR = 红色 x4)。我当前使用的代码是:

private static boolean isStreak(int color, int... lst){
    for(int i:lst){
        if(i!=color){//Whichever color is being checked
            if(i!=-12280508)//Green counts as whichever color is being checked
                return false;
        }
    }
    return true;
}

虽然此代码有效,但我需要几个 if/else block 来有效捕获 1-7 中的所有条纹,例如:

if(isStreak(Color.RED.value,last[lSize-1],last[lSize-2],last[lSize-3],last[lSize-4],last[lSize-5],last[lSize-6],last[lSize-7])){
    if(!lb.was7){
        log("red 7x");
        doBet(bet*64);
        betBlack();
        lb.was7=true;
    }
}else if(isStreak(Color.BLACK.value,last[lSize-1],last[lSize-2],last[lSize-3],last[lSize-4],last[lSize-5],last[lSize-6],last[lSize-7])){
    if(!lb.was7){
        log("red 7x");
        doBet(bet*64);
        betRed();
        lb.was7=true;
    }
}else if(isStreak(Color.RED.value,last[lSize-1],last[lSize-2],last[lSize-3],last[lSize-4],last[lSize-5],last[lSize-6])){
    log("red 6x");
    doBet(bet*32);
    betBlack();
}else if(isStreak(Color.BLACK.value,last[lSize-1],last[lSize-2],last[lSize-3],last[lSize-4],last[lSize-5],last[lSize-6])){
    log("black 6x");
    doBet(bet*32);
    betRed();
}

等等。我想知道是否有一种方法可以编写一些代码来接受列表的参数,例如:

getStreak(int[] list)

并且会返回条纹的长度和条纹的颜色。我不知道该去哪里或从哪里开始,所以任何帮助将不胜感激。

提前致谢!

最佳答案

总体思路是存储当前所在条纹的颜色以及该条纹的长度。循环遍历列表中的颜色,对于每一种颜色,如果它与当前条纹的颜色相同或者是绿色,则增加当前条纹计数器。一旦连续结束,检查它是否比之前最长的连续记录更长(您必须跟踪)。如果是,则更新最长的条纹及其颜色,重置当前的条纹,并继续寻找更长的条纹。

以下是一些代码,您可以将其放入 getStreak(int[] Colors) 方法中来完成此操作:

if (colors != null && colors.length > 0) {
    // Default streaks to 1, and colors to the first color
    int longestStreak = 1;
    int longestStreakColor = colors[0];
    int currStreak = 1;
    int currStreakColor = colors[0];

    // Start at index 1 since we already stored the first color in currStreakColor
    for (int i = 1; i < colors.length; i++) {
        // If the color is the same as the previous color or is green,
        // increment the currStreak
        if (colors[i] == currStreakColor || colors[i] == Color.GREEN.value) {
            currStreak++;
        } else {
            // If the color is different and the streak that just ended was
            // longer than the previous longest streak, update longestStreak
            // and longestStreakColor
            if (currStreak > longestStreak) {
                longestStreak = currStreak;
                longestStreakColor = currStreakColor;
            }
            // Reset currStreak
            currStreak = 1;
        }
        // Set currStreakColor to the current color if it isn't green
        if (colors[i] != Color.GREEN.value) {
            currStreakColor = colors[i];
        }
    }
}

// If we ended on a streak longer than the previous longest streak, update
// longestStreak and longestStreakColor
if (currStreak > longestStreak) {
    longestStreak = currStreak;
    longestStreakColor = currStreakColor;
}

如果您希望在方法中使用此代码,以返回颜色和条纹,您可能必须创建一个具有两个 int 字段的类,例如 ColorStreak ,一个表示颜色,一个表示条纹的长度。然后,从 longestStreaklongestStreakColor 创建一个新的 ColorStreak 并返回它(因为函数只能返回一个值)。

关于java - 从列表中获取最长的连续记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34114356/

相关文章:

Java-如何在换行前显示数字 5 次

java - 使用三元运算符与 if else 与 switch case 的比较(性能)

java - 如何保护基于客户端-服务器的 Java 桌面应用程序的数据库凭据

java - 如何将 Spring 依赖项注入(inject)到消息驱动 EJB 中?

java - 使用 java 将附件上传到 zendesk

python - 从对象列表中获取值

LINQ - 组合多个列表以形成一个新列表并按键对齐?

java - 集合中类的访问方法

java - 如何验证字符串是否包含列表中的任何字符串?

c - fscanf 不读取文件