暴力解法
public class test {
public String nextClosedTime(String s){
int[] a = new int[4];
int[][] result = new int[256][4];
int[] time1 = new int[256];
int count = 0;
int index = Integer.MAX_VALUE;
int resultIndex = Integer.MAX_VALUE;
String p = "";
a[0] = s.charAt(0) - '0';
a[1] = s.charAt(1) - '0';
a[2] = s.charAt(3) - '0';
a[3] = s.charAt(4) - '0';
/**
* 列举所有可能性
*/
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
for(int k=0; k<4; k++){
for(int t=0; t<4; t++){
result[count][0] = a[i];
result[count][1] = a[j];
result[count][2] = a[k];
result[count][3] = a[t];
count++;
}
}
}
}
/**
* 清除不合理的数值
*/
for(int i=0; i<256; i++){
if(result[i][0] > 2 || (result[i][0]==2 && result[i][1]>3) || result[i][2]>5){
result[i][0] = Integer.MAX_VALUE;
result[i][1] = Integer.MAX_VALUE;
result[i][2] = Integer.MAX_VALUE;
result[i][3] = Integer.MAX_VALUE;
}
}
/**
* 记录时间
*/
for(int i=0; i<256; i++){
if(result[i][0] == Integer.MAX_VALUE){
time1[i] = Integer.MAX_VALUE;
}else{
time1[i] = result[i][0]*60*10 + result[i][1]*60 +result[i][2]*10 + result[i][3];
time1[i] = time1[i] - (a[0]*60*10 + a[1]*60 + a[2]*10 + a[3]);
if(time1[i] <= 0){
time1[i] += 24*60;
}
}
}
/**
* 获取最小时间差
*/
for(int i=0; i<256; i++){
if(time1[i] < resultIndex){
resultIndex = time1[i];
index = i;
}
}
if(index < 256){
p += result[index][0] + result[index][1] + ":" + result[index][2] + result[index][3];
}
return p;
}
}