i have another way in python, a little longger when i wrote


  • 0

    Re: Short Python

    your method is real good!
    i have a method, although it's not small ,it also can slove the problem

    Perimeter = X span + Y span + 2* (splitor nums in X and Y)
    in one line , every time you split a line ,you got another two edge

    
            Xmin, Xmax, Xspace=100, 0, 0
            Ymin, Ymax, Yspace=100, 0, 0
            for y in xrange(len(grid)):
                s= ''
                for x in xrange(len(grid[y])):
                    if grid[y][x] == 1:
                        if x<Xmin : Xmin = x
                        if x>Xmax : Xmax = x
                        if y<Ymin : Ymin = y
                        if y>Ymax : Ymax = y
                        s += '1'
                    else: s += ' '
                if s.strip() == '': continue
                Xspace = Xspace +len(s.split()) - 1
            grid1=map(list, zip(*grid))
            for x in xrange(len(grid1)):
                s=''
                for y in xrange(len(grid1[x])):
                    if grid1[x][y]== 1:
                        s += '1'
                    else: s += ' '
                if s.strip() == '': continue
                Yspace=Yspace + len(s.split()) - 1
            return 2 * (Xmax - Xmin+1) + 2 * (Ymax - Ymin+1) + 2 * (Xspace + Yspace)
    

Log in to reply
 

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