[C++] [Java] Clean Code


  • 16

    C++

    class Solution {
    public:
        string findContestMatch(int n) {
            vector<string> m(n);
            for (int i = 0; i < n; i++) {
                m[i] = to_string(i + 1);
            }
    
            while (n > 1) {
                for (int i = 0; i < n / 2; i++) {
                    m[i] = "(" + m[i] + "," + m[n - 1 - i] + ")";
                }
                n /= 2;
            }
            
            return m[0];
        }
    };
    

    Java

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

Log in to reply
 

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