//Topcoder SRM 575 DIV 2 L1 TheSwapsDivTwo
import java.util.*;
import java.math.*;
//rename the class name before submit
public class TheSwapsDivTwo {
public static void main(String[] args) {
TheSwapsDivTwo obj = new TheSwapsDivTwo();
System.out.println(
obj.find(
new int[] { 4, 7, 4 }
));
}
public int find(int[] sequence) {
HashSet<String> set = new HashSet<String>();
for (int i = 0; i < sequence.length; i++) {
for (int j = i + 1; j < sequence.length; j++) {
int temp = sequence[i];
sequence[i] = sequence[j];
sequence[j] = temp;
StringBuilder sb = new StringBuilder();
for (int k = 0; k < sequence.length; k++) {
sb.append("-" + String.valueOf(sequence[k]));
}
if (!set.contains(sb)) {
set.add(sb.toString());
}
temp = sequence[i];
sequence[i] = sequence[j];
sequence[j] = temp;
}
}
return set.size();
}
}
No comments:
Post a Comment