alives
BAN USERAm I off track here?
private static void runPairs(int[] list) {
int len = list.length;
for (int i=0;i<len;i++) {
if (Arrays.binarySearch(list,list[i]+1) >0 && list[i] != 1) {
System.out.print(list[i]+"-");
} else if (list[i] > 1) {
System.out.print(list[i]);
if (i<len-1) System.out.print(",");
}
}
}
runPairs(new int[] {0,1,2,7,21,22,1098,1099});
private static Node root;
private static Node[] max = new Node[2];
public static void main(String[] args) {
int[] arr = {0, 0, 100, 3, 5, 4, 6, 4, 2, 100, 2, 100};
int len = arr.length;
for (int i=0;i<len;i++) {
push(arr[i]);
}
System.out.println("["+max[0].key+","+max[1].key+"]");
}
private static void push(int i) {
root = push(root, i);
}
private static Node push(Node n, int i) {
if (n == null) return new Node(i, 1);
if (n.key > i || n.key < i) n.next = push(n.next, i);
else if (n.key == i) n.count++;
makeMaxArray(n);
return n;
}
private static void makeMaxArray(Node n) {
if (max[0] == null) max[0] = n;
if (n.count >= max[0].count) {
Node tmp = max[0];
max[0] = n;
max[1] = tmp;
}
}
private static class Node {
int key;
int count = 0;
Node next;
private Node(int i, int cnt) {
this.key = i;
this.count += cnt;
}
}
public static void outputPairs(int n) {
int lst = 1;
for (int i=1;i<n;i++) {
int fb = fib(i);
int b = lst;
lst = fb%10;
System.out.println("[" + b + ", " + lst + "]");
}
}
private static int fib(int n) {
if(n <= 1){
return n;
}
int fibo = 1;
int fiboPrev = 1;
for(int i = 2; i < n; ++i){
int temp = fibo;
fibo += fiboPrev;
fiboPrev = temp;
}
return fibo;
}
- alives April 02, 2015