Simple Java Recursive Solution With No Extra Space


  • 2
    W
    
        public String findContestMatch(int n) {
            return generateMatch(n, 1, 2);
        }
        
        private String generateMatch(int n, int top, int level){
            if (level == n)
                return "(" + top +"," + (level + 1 - top) + ")";
            return "(" + generateMatch(n, top, level << 1) +"," + generateMatch(n, level + 1 - top, level << 1) + ")";
        }
    

Log in to reply
 

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