-
-
Save mindwing/80531f5442e0c745d8b2 to your computer and use it in GitHub Desktop.
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
import java.util.ArrayList; | |
/** | |
* Created by mindwing on 2016-01-02. | |
* 완전순열 | |
*/ | |
public class Test { | |
public static void main(String[] args) throws Exception { | |
ArrayList<String> arr = new ArrayList<>(); | |
perm("", "1234", arr); | |
System.out.println(arr.size()); | |
System.out.println(arr); | |
} | |
public static void perm(String head, String input, ArrayList<String> arr) throws Exception { | |
if (input.length() <= 1) { | |
arr.add(head + input); | |
return; | |
} | |
for (int i = 0; i < input.length(); i++) { | |
// substring(자를 위치 첨자, 첨자에서 자를만큼의 길이) or substring(자를 위치 첨자) | |
String newStr = input.substring(0, i) + input.substring(i + 1); | |
// perm() 메서드 안에서 다시 perm() 메서드를 호출하는 것을 재귀호출이라고 합니다. | |
perm(head + input.charAt(i), newStr, arr); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment