QAQ
BAN USERpackage zhang.juanyong.algorithms2;
import java.util.Queue;
import org.apache.commons.lang.StringUtils;
public class VersionComparator {
public static int compareVersion(String v1, String v2) {
Queue<Integer> q1 = new java.util.LinkedList<Integer>();
Queue<Integer> q2 = new java.util.LinkedList<Integer>();
loadVersion(q1, v1);
loadVersion(q2, v2);
int comp = 0;
while (!q1.isEmpty()) {
comp = q1.poll().compareTo(q2.poll());
if (0 != comp) {
return comp;
}
}
return comp;
}
private static void loadVersion(Queue<Integer> q, String version) {
String[] vNums = StringUtils.split(version, ".");
for (String v : vNums) {
q.add(Integer.valueOf(v));
}
}
public static void main(String[] args) {
String v1 = "10.3.4";
String v2 = "10.3.41";
System.out.println(String.format("compareVersion(%s, %s)=%s", v1, v2,
compareVersion(v1, v2)));
}
}
package zhang.juanyong.interview.amazon;
import java.util.Arrays;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Set;
import org.apache.commons.lang.ArrayUtils;
/**
* Provided a number dictionary and a number, x , which is formed from the
* number dictionary. Find the rank of the number x? Rank is defined as the
* position of the number x when all the number formed from the dictionary are
* sorted.
*
* Example Input :{4,1,5} X : 451
*
* Output : 4
*
* (145,154,415,451,514,541). 451 comes at 4th position
*
* @author Juanyong
*
*/
public class NumberDictionary2 {
public static void main(String[] args) {
NumberDictionary2 nd2 = new NumberDictionary2();
nd2.permutations(new Integer[] { 4, 1, 5 }, 0);
Object find = 451;
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
pq.addAll(nd2.unique);
Object[] ary = nd2.unique.toArray();
Arrays.sort(ary);
System.out.println(Arrays.toString(ary));
System.out.println(ArrayUtils.indexOf(ary, find)+1+"th");
}
Set<Integer> unique = new HashSet<Integer>();
private void permutations(Integer[] args, int start) {
// System.out.println(Arrays.toString(args));
for (int i = start; i < args.length; i++) {
for (int j = i; j < args.length; j++) {
if (i != j) {
swap(args, i, j);
System.out.println(Arrays.toString(args));
unique.add(sum(args));
permutations(args, start + 1);
swap(args, j, i);
}
}
}
}
private Integer sum(Integer[] args) {
String value = "";
for (Integer digi : args) {
value += digi;
}
return Integer.valueOf(value);
}
private static void swap(Integer[] args, int i, int j) {
if (i == j)
return;
Integer tmp = args[i];
args[i] = args[j];
args[j] = tmp;
}
}
- QAQ May 15, 2014