5 lines Python


  • 8

    Pretty straight forward one, just keep going until n == 1.

    class Solution(object):
        def findContestMatch(self, n):
            """
            :type n: int
            :rtype: str
            """
            res = map(str, range(1, n+1))
            while n > 1:
                res = ["(" + res[i] + "," + res[n-1-i] + ")" for i in range(n >> 1)]
                n >>= 1
            return res[0]
    

  • 0
    S

    @realisking Thanks, I had another method for python...

    class Solution(object):
    def findContestMatch(self, n):
    """
    :type n: int
    :rtype: str
    """

        t=[a for a in range(1,n+1)]
        
        while len(t)!=1:
    		c = t[0:(len(t)//2)]
    		d = t[len(t) : len(t)//2-1 : -1 ]
    		t=(zip(c,d))
        return("".join(str(t[0]).split(' ')))

Log in to reply
 

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