有 Java 编程相关的问题?

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

使Java Pebble纸牌程序中的代码更有效

如何使此代码更有效(更快)

这是一个叫做“卵石纸牌”的游戏。它与Peg纸牌相同,但只有一条线(鹅卵石不放在十字架上)

如果是“oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo

如何才能变得更有效

public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);

        long startTime = System.currentTimeMillis();

        posSol = new ArrayList<>();
        posSol.add(reader.nextLine());

        int minimumNoOfPebbles = 23;


        for (int y = 0; y < posSol.size(); y++) {
            String s = posSol.get(y);

            for (int i = 0; i <= s.length() - 1; i++) {
                if (s.charAt(i) == 'o') {
                    if (i <= s.length() - 2 && s.charAt(i + 1) == 'o' && s.charAt(i + 2) == '-') {
                        String temp = s.substring(0, i) + "--o" + s.substring(i + 3);
                        posSol.add(temp);
                    }
                    if (i >= 2 && s.charAt(i - 1) == 'o' && s.charAt(i - 2) == '-') {
                        String temp = s.substring(0, i - 2) + "o--" + s.substring(i + 1);
                        posSol.add(temp);
                    }
                }
            }

            int counter = 0;
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == 'o') {
                    counter++;
                }
            }
            if (minimumNoOfPebbles > counter) {
                minimumNoOfPebbles = counter;
            }
        }

        System.out.println(minimumNoOfPebbles);

        long endTime   = System.currentTimeMillis();
        long totalTime = endTime - startTime;
        System.out.println(totalTime);

    }

共 (0) 个答案