Last active
May 24, 2017 17:45
-
-
Save 2efPer/a3d755496714f4165789b4383819e98e to your computer and use it in GitHub Desktop.
Combination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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