Friday, July 26, 2013

Topcoder SRM 583 DIV 2 L1 SwappingDigits

// Topcoder SRM 583 DIV 2 L1 SwappingDigits

import java.util.*;
import java.math.*;

//tc; rename the class name before submitting
public class SwappingDigits {
public static void main(String[] args) {
SwappingDigits obj = new SwappingDigits();
System.out.println(
obj.minNumber(
"5491727514"
));
}

public String minNumber(String num) {
char[] c = num.toCharArray();
for (int i = 0; i < c.length; i++) {
char minx = c[i];
for (int j = c.length - 1; j > i; j--) {
if (c[j] < minx) {
if (i > 0)
minx = c[j];
else if (c[j] > '0')
minx = c[j];
}
}
if (minx == c[i])
continue;

for (int j = c.length - 1; j > i; j--) {
if (c[j] == minx) {
char tmp = c[i];
c[i] = c[j];
c[j] = tmp;
return new String(c);
}
}
}
return new String(c);
}
}

No comments:

Post a Comment