Storage

Array

index
11 12 3 4 5 6 7 8 9 10
//Use an insert position index to track where the next element goes.
//this is to maintain a stream of 10 numbers sliding window
index = (index+1) % nums.length;

static final array

use static final array to store some quick mapping information.

private static final char[][] pairs = {{'0', '0'}, {'1', '1'}, {'6', '9'}, {'8', '8'}, {'9', '6'}};

Save space by overwrite useless data

Let say you have n elements, after one round of computation, you got n/2 results. Your next round of computation depends on that n/2 new elements only. We could reuse the original array by overwriting the first n/2 old elements.

In this way, we don't need two queues.

while(n>1) {
    for(int i=0; i<n/2; i++) {
        result[i] = "(" + result[i] + "," + result[n-1-i] + ")";
    }
    n /= 2;
}
return result[0];

results matching ""

    No results matching ""