You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package 02.mca_01;
public class Problem04_MinSwapStep {
// 一个数组中只有两种字符'G'和'B'
// 可以让所有的G都放在左侧所有的B都放在右侧
// 或者可以让所有的G都放在右侧所有的B都放在左侧
// 但是只能在相邻字符之间进行交换操作,请问请问至少需要交换几次,
public static int minSteps1(String s) {
if (s == null || s.equals("")) {
return 0;
}
char[] str = s.toCharArray();
int step1 = 0;
int gi = 0;
for (int i = 0; i < str.length; i++) {
if (str[i] == 'G') {
step1 += i - (gi++);
}
}
int step2 = 0;
int bi = 0;
for (int i = 0; i < str.length; i++) {
if (str[i] == 'B') {
step2 += i - (bi++);
}
}
return Math.min(step1, step2);
}
// 可以让G在左或者在右
public static int minSteps2(String s) {
if (s == null || s.equals("")) {
return 0;
}
char[] str = s.toCharArray();
int step1 = 0;
int step2 = 0;
int gi = 0;
int bi = 0;
for (int i = 0; i < str.length; i++) {
if (str[i] == 'G') { // 当前的G去左边 方案1
step1 += i - (gi++);
} else {// 当前的B去左边 方案2
step2 += i - (bi++);
}
}
return Math.min(step1, step2);
}
// 为了测试
public static String randomString(int maxLen) {
char[] str = new char[(int) (Math.random() * maxLen)];
for (int i = 0; i < str.length; i++) {
str[i] = Math.random() < 0.5 ? 'G' : 'B';
}
return String.valueOf(str);
}
public static void main(String[] args) {
int maxLen = 100;
int testTime = 1000000;
System.out.println("测试开始");
for (int i = 0; i < testTime; i++) {
String str = randomString(maxLen);
int ans1 = minSteps1(str);
int ans2 = minSteps2(str);
if (ans1 != ans2) {
System.out.println("Oops!");
}
}
System.out.println("测试结束");
}
}