Java 如何在不使用多线程的情况下加快速度?
就输出而言,我的程序运行良好,但对于我的一些测试用例来说,找到答案需要花费太长时间(有时需要18秒)。我想知道如何提高代码的性能 我的代码的作用是: 这是一个挑战。用户输入n个游戏,然后输入长度为23的字符串,其中仅包含“o”(圆石)和“-”(空白)的组合。如果有两个相邻的鹅卵石,两边都有一个空的空间,即(oo-或-oo),那么你移除中间的鹅卵石,然后交换另外两块鹅卵石,ex'oo-'将变成'--o' 我目前的方法几乎是一种穷尽的方法,它尝试了每一个可能的移动,并以最少的鹅卵石数完成移动集 我想知道如何改进此解决方案而不使其成为多线程的 以下是我所拥有的:Java 如何在不使用多线程的情况下加快速度?,java,performance,Java,Performance,就输出而言,我的程序运行良好,但对于我的一些测试用例来说,找到答案需要花费太长时间(有时需要18秒)。我想知道如何提高代码的性能 我的代码的作用是: 这是一个挑战。用户输入n个游戏,然后输入长度为23的字符串,其中仅包含“o”(圆石)和“-”(空白)的组合。如果有两个相邻的鹅卵石,两边都有一个空的空间,即(oo-或-oo),那么你移除中间的鹅卵石,然后交换另外两块鹅卵石,ex'oo-'将变成'--o' 我目前的方法几乎是一种穷尽的方法,它尝试了每一个可能的移动,并以最少的鹅卵石数完成移动集 我想
package Pebble;
import java.util.Scanner;
public class PebbleSolitaire {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int numOfGames = Integer.parseInt(input.nextLine());
while (numOfGames > 0){
char[] values = input.nextLine().toCharArray();
long startTime = System.nanoTime();
System.out.println(solve(values));
System.out.println("Time to finish in ms: " + (System.nanoTime() - startTime) / 1000000);
numOfGames--;
}
input.close();
}
private static int solve(char[] game){
if(game != null && game.length == 0){
return -1;
}
int result = 0;
for (int i = 0; i < game.length; i++){
if(game[i] == 'o'){
result++;
}
}
//print(game);
for (int i = 0; i < game.length; i++ ){
char[] temp = new char[game.length];
copyArray(temp, game);
if (i-2 >= 0 && temp[i] == '-' && temp[i-2] == 'o' && temp[i-1] == 'o'){//move pebble forwards
temp[i-1] = temp[i-2] = '-';
temp[i] = 'o';
result = Math.min(result, solve(temp));
}
copyArray(temp, game);
if(i+2 < temp.length && temp[i] == '-' && temp[i+1] == 'o' && temp[i+2] == 'o'){//move pebble backwards
temp[i+1] = temp[i+2] = '-';
temp[i] = 'o';
result = Math.min(result, solve(temp));
}
}
return result;
}
private static void copyArray(char[] copy, char[] og){
for(int x = 0; x < copy.length; x++){
copy[x] = og[x];
}
}
private static void print(char[] c){
for(char ch: c){
System.out.print(ch);
}
System.out.println();
}
}
编辑:将此完全迭代会显著提高性能吗?也许您可以改进此部分:
for (int i = 0; i < game.length; i++ ){
char[] temp = new char[game.length];
copyArray(temp, game);
if (i-2 >= 0 && temp[i] == '-' && temp[i-2] == 'o' && temp[i-1] == 'o'){//move pebble forwards
temp[i-1] = temp[i-2] = '-';
temp[i] = 'o';
result = Math.min(result, solve(temp));
}
copyArray(temp, game);
if(i+2 < temp.length && temp[i] == '-' && temp[i+1] == 'o' && temp[i+2] == 'o'){//move pebble backwards
temp[i+1] = temp[i+2] = '-';
temp[i] = 'o';
result = Math.min(result, solve(temp));
}
}
for(int i=0;i=0&&temp[i]='-'&&temp[i-2]=='o'&&temp[i-1]='o'){//向前移动鹅卵石
温度[i-1]=温度[i-2]='-';
温度[i]=“o”;
结果=数学最小值(结果,求解(温度));
}
复制阵列(临时、游戏);
如果(i+2
致:
for(int i=0;i=0&&game[i]='-'&&game[i-2]=='o'&&game[i-1]='o'){//向前移动鹅卵石
temp=新字符[游戏长度];
复制阵列(临时、游戏);
温度[i-1]=温度[i-2]='-';
温度[i]=“o”;
结果=数学最小值(结果,求解(温度));
}
如果(i+2
基本上,仅在严格必要时创建和“copyArray(临时、游戏);”
for (int i = 0; i < game.length; i++ ){
char[] temp = new char[game.length];
copyArray(temp, game);
if (i-2 >= 0 && temp[i] == '-' && temp[i-2] == 'o' && temp[i-1] == 'o'){//move pebble forwards
temp[i-1] = temp[i-2] = '-';
temp[i] = 'o';
result = Math.min(result, solve(temp));
}
copyArray(temp, game);
if(i+2 < temp.length && temp[i] == '-' && temp[i+1] == 'o' && temp[i+2] == 'o'){//move pebble backwards
temp[i+1] = temp[i+2] = '-';
temp[i] = 'o';
result = Math.min(result, solve(temp));
}
}
for (int i = 0; i < game.length; i++ ){
char[] temp = null;
if (i-2 >= 0 && game[i] == '-' && game[i-2] == 'o' && game[i-1] == 'o'){//move pebble forwards
temp = new char[game.length];
copyArray(temp, game);
temp[i-1] = temp[i-2] = '-';
temp[i] = 'o';
result = Math.min(result, solve(temp));
}
if(i+2 < game.length && game[i] == '-' && game[i+1] == 'o' && game[i+2] == 'o'){//move pebble backwards
if(temp == null) temp = new char[game.length];
copyArray(temp, game);
temp[i+1] = temp[i+2] = '-';
temp[i] = 'o';
result = Math.min(result, solve(temp));
}
}