A/C Python solution, beat 74.14%, easy to understand


  • 0
    W

    class Solution(object):
    def isConvex(self, points):
    """
    :type points: List[List[int]]
    :rtype: bool
    """
    #print "points = ", points

        numPoints = len(points)
        #print "numPoints = ", numPoints
    
        pre, cur = 0, 0
        for idx in range(numPoints):
    
            #print "idx = ", idx, " points[idx] = ", points[idx]
    
            dx1 = points[(idx+1)%numPoints][0] - points[idx][0]
            dy1 = points[(idx+1)%numPoints][1] - points[idx][1]
    
            dx2 = points[(idx+2)%numPoints][0] - points[idx][0]
            dy2 = points[(idx+2)%numPoints][1] - points[idx][1]
    
            #print "dx1 = ", dx1, " dy1 = ", dy1
            #print "dx2 = ", dx2, " dy2 = ", dy2
    
            cur = dx1*dy2 - dx2 * dy1
            #print "cur = ", cur
            if cur != 0:
                if cur*pre < 0:
                    return False
                else:
                    pre = cur
    
        return True

Log in to reply
 

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