Short Recursive Solution


  • 0
    G
    public class Solution {
        public String findContestMatch(int n) {
            List<String> nums = new ArrayList<>();
            for (int i = 1; i <= n; i++) {
                nums.add(Integer.toString(i));
            }
            return build(nums).get(0);
        }
        
        public List<String> build(List<String> list) {
            int n = list.size(); 
            if (n == 1) { return list; }
            List<String> newList = new ArrayList<>();
            for (int i = 0; i < n / 2; i++) {
                newList.add("(" + list.get(i) + "," + list.get(n - i - 1) + ")");
            }
            return build(newList);
        }
    }
    
    

Log in to reply
 

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