Skip to content

Instantly share code, notes, and snippets.

@czxttkl
Created October 11, 2014 02:49
Show Gist options
  • Save czxttkl/f783c49a832289dd695a to your computer and use it in GitHub Desktop.
Save czxttkl/f783c49a832289dd695a to your computer and use it in GitHub Desktop.
combination
import java.util.Arrays;
public class TestAnything {
public static void main(String[] args) throws Exception {
int[] seqs = new int[] { 1, 2, 3, 4, 5, 6 };
int[] pointers = new int[] { 0, 1, 2 };
while (!isExhausted(pointers, seqs.length)) {
System.out.println(Arrays.toString(pointers));
updatePointers(pointers, seqs.length, pointers.length - 1);
}
}
private static boolean isExhausted(int[] pointers, int length) {
for (int i = pointers.length - 1; i >= 0; i--) {
if (pointers[i] != length - (pointers.length - i)) {
return false;
}
}
return true;
}
private static void updatePointers(int[] pointers, int length, int curPos) {
if (pointers[curPos] + 1 <= length - 1) {
pointers[curPos]++;
} else {
updatePointers(pointers, length - 1, curPos - 1);
pointers[curPos] = pointers[curPos - 1] + 1;
}
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment