Java Iterative solution


  • 0
    C
        public String findContestMatch(int n) {
            List<String> lineUp = new ArrayList<String>();
            for(int i=1; i<=n; i++){
                lineUp.add(String.valueOf(i));
            }
           
            while(lineUp.size() > 1){
                int beg = 0, end = lineUp.size() - 1;
                List<String> nextRound = new ArrayList<String>();
                while(beg < end){
                    nextRound.add("(" + lineUp.get(beg++) + "," + lineUp.get(end--) + ")");
                }
                
                lineUp = nextRound;
            }
            
            return lineUp.get(0);
        }
    

Log in to reply
 

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