memorandum

Code memorandum

Useful code sample found on the net

Enumeration of all combinations

The source is here Click here for commentary (https://qiita.com/masakinpo/items/b6ae147d9a61b592240a)

·permutation

    public static void permutation(String q, String ans){
        // Base Case
        if(q.length() <= 1) {
            System.out.println(ans + q);
        }
        // General Case
        else {
            for (int i = 0; i < q.length(); i++) {
                permutation(q.substring(0, i) + q.substring(i + 1),
                        ans + q.charAt(i));
            }
        }
    }

・ Combination

    public static void combination(String q, String ans, int k){
        // Base Case
        if(ans.length() == k) {
            System.out.println(ans);
        }
        // General Case
        else {
            while (q.length() > 0) {
                combination(q.substring(1), ans + q.charAt(0), k);
                q = q.substring(1);
            }
        }
    }
    public static void main(String[] args) {
        combination("abcd", "", 2);
    }

-Treat the second argument ans like a private constructor


public static void permutation(String q){
  permutation(q, "");
}

private static void permutation(String q, String ans){
 //省略
}

Recommended Posts

memorandum
memorandum
MyBatis memorandum
Java memorandum
Rails memorandum
Jaba Jaba Memorandum
JavaFx memorandum
JAVA memorandum
JUnit memorandum
Docker memorandum
Gradle Memorandum
Docker memorandum
thymeleaf memorandum
Technical memorandum (Docker)
Log Level Memorandum
Memorandum docker command
Apache FreeMarker memorandum
Java memorandum (list)
Screen transition memorandum
Rails tutorial memorandum 1
[Ruby] Method memorandum
Rails tutorial memorandum 2
Docker command memorandum
Java study memorandum
Spring Boot Memorandum
[Java] Optional memorandum
Memorandum (Spring Web)
Docker Machine (personal memorandum)
Javasilver study session memorandum
Rails version upgrade memorandum 5.0 → 5.1
Recently learned method, memorandum ①
webApi memorandum in java
Rails Tutorial Memorandum (Chapter 3, 3.1)
Total quantity selected memorandum
Java8 Silver exam memorandum
ruby memorandum (basic edition)
Java8 Gold exam memorandum
Docker network (personal memorandum)
Rails Tutorial Memorandum (Chapter 3, 3.3.2)