Skip to content

Instantly share code, notes, and snippets.

@2efPer
Last active May 24, 2017 17:45
Show Gist options
  • Select an option

  • Save 2efPer/a3d755496714f4165789b4383819e98e to your computer and use it in GitHub Desktop.

Select an option

Save 2efPer/a3d755496714f4165789b4383819e98e to your computer and use it in GitHub Desktop.
Combination
class CombinationTest{
public static void main(String[] args) {
listCombination(new char[]{'a','b','c'});
}
public static void listCombination(char chs[]){
if(chs.length == 0) return ;
Stack<Character> stack = new Stack<Character>();
for(int i = 1; i <= chs.length; i++){
combine(chs, 0, i, stack);
}
}
public static void combine(char []chs, int begin, int number, Stack<Character> stack){
if(number == 0){
System.out.println(stack.toString());
return ;
}
if(begin == chs.length){
return;
}
stack.push(chs[begin]);
combine(chs, begin + 1, number - 1, stack);
stack.pop();
combine(chs, begin + 1, number, stack);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment