Clean and short Java solution with two array


  • 0
    Y
    public class Solution {
        public String findContestMatch(int n) {
            String[] strArr = new String[n];
            for (int i = 1; i <= n; ++i) {
                strArr[i - 1] = String.valueOf(i);
            }
            while (n > 1) {
                String[] nextArr = new String[n / 2];
                for (int l = 0, r = n - 1; l < r; ++l, --r) {
                    nextArr[l] = "(" + strArr[l] + "," + strArr[r] + ")";
                }
                strArr = nextArr;
                n /= 2;
            }
            return strArr[0];
        }
    }
    

Log in to reply
 

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