题解 | 乒乓球

乒乓球

https://www.nowcoder.com/practice/78660925b1cd49b6b2e43cb375ed7945

import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.next();
        List<String>result1 = solve(s, 11);
        List<String>result2 = solve(s, 21);
        for(String cur:result1){
            System.out.println(cur);
        }
        System.out.println();
        for(String cur:result2){
            System.out.println(cur);
        }

    }
    private static List<String> solve(String s, int limit) {
        List<String>result = new ArrayList<>();
        int win = 0, lose = 0;
        for (int i = 0; i < s.length(); i++) {
            char cur = s.charAt(i);
            if (cur == 'W')win++;
            if (cur == 'L')lose++;
            if ((win >= limit || lose >= limit) && Math.abs(win - lose) >= 2) {
                result.add(win + ":" + lose);
                win = 0;
                lose = 0;
            }
        }
        result.add(win + ":" + lose);
        return result;
    }
}

全部评论

相关推荐

03-31 21:47
东南大学 C++
彭于晏前来求offe...:吓晕了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务