Java Recursive Solution


  • 1
    J

    I know the space complexity is not good, but this solution is quite straight forward.

    public class Solution {

    public String findContestMatch(int n) {
        List<String> s = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            s.add(i + "");
        }
        get(n, s);
        return s.get(0);
    }
    
    public void get(int n, List<String> s) {
        if (n == 1) return;
        for (int i = 0; i < n; i++) {
            s.set(i, "(" + s.get(i) + "," + s.get(n - i - 1) + ")");
        }
        get(n / 2, s);
    }
    

    }


  • 0
    U

    nice solution, you can loop just up to n/2 in get() actually


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.