modify code

master
algorithmzuo 3 years ago
parent 163da249f0
commit 0fce18e785

@ -48,7 +48,7 @@ public class Code01_Light {
light++;
if (i + 1 == str.length) {
break;
} else { // 有i位置 i+ 1 X .
} else { // 有i位置 i+ 1 X .
if (str[i + 1] == 'X') {
i = i + 2;
} else {
@ -60,6 +60,25 @@ public class Code01_Light {
return light;
}
// 更简洁的解法
// 两个X之间数一下.的数量然后除以3向上取整
// 把灯数累加
public static int minLight3(String road) {
char[] str = road.toCharArray();
int cur = 0;
int light = 0;
for (char c : str) {
if (c == 'X') {
light += (cur + 2) / 3;
cur = 0;
} else {
cur++;
}
}
light += (cur + 2) / 3;
return light;
}
// for test
public static String randomString(int len) {
char[] res = new char[(int) (Math.random() * len) + 1];
@ -76,7 +95,8 @@ public class Code01_Light {
String test = randomString(len);
int ans1 = minLight1(test);
int ans2 = minLight2(test);
if (ans1 != ans2) {
int ans3 = minLight3(test);
if (ans1 != ans2 || ans1 != ans3) {
System.out.println("oops!");
}
}

Loading…
Cancel
Save