xelways
BAN USERstatic class LocationColorPair {
int location, color;
}
static LocationColorPair countLocationColor(String search, String find) {
if(search == null || find == null || search.length() != find.length()) return null;
LocationColorPair result = new LocationColorPair();
int[] available = new int[4];
int[] machable = new int[4];
for(int pos = 0; pos < search.length(); pos++) {
if(search.charAt(pos) == find.charAt(pos)) {
result.location++;
} else {
available[mapToIndex(search.charAt(pos))]++;
machable[mapToIndex(find.charAt(pos))]++;
}
}
for(int i=0; i<4; i++) {
result.color += Math.min(available[i], machable[i]);
}
return result;
}
static int mapToIndex(char ch) {
switch(ch) {
case 'B': return 0;
case 'G': return 1;
case 'R': return 2;
case 'Y': return 3;
default:
throw new IllegalArgumentException();
}
}
public static void twoCubesSum(int n) {
if(n < 28) return;
double cubeRoot = Math.cbrt(n);
int fwd = 1;
int bck = (int) cubeRoot;
int count = 2;
while(fwd < bck) {
int cubeFwd = fwd * fwd * fwd;
int cubeBck = bck * bck * bck;
if(cubeFwd + cubeBck == n) {
if(count == 2) {
System.out.println("First pair: " + fwd + ", " + bck);
count--;
} else {
System.out.println("Second pair: " + fwd + ", " + bck);
return;
}
}
if(cubeFwd + cubeBck >= n) {
bck--;
} else {
fwd++;
}
}
}
public static void twoCubesSum(int n) {
if(n < 28) return;
double cubeRoot = Math.cbrt(n);
int fwd = 1;
int bck = (int) cubeRoot;
int count = 2;
while(fwd < bck) {
int cubeFwd = fwd * fwd * fwd;
int cubeBck = bck * bck * bck;
if(cubeFwd + cubeBck == n) {
if(count == 2) {
System.out.println("First pair: " + fwd + ", " + bck);
count--;
} else {
System.out.println("Second pair: " + fwd + ", " + bck);
return;
}
}
if(cubeFwd + cubeBck >= n) {
bck--;
} else {
fwd++;
}
}
}
- xelways March 16, 2015