Skip to content

Latest commit

 

History

History

600

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

LeetCode 551-600

551 Student Attendance Record I

class Solution(object):
    def checkRecord(self, s):
        """
        :type s: str
        :rtype: bool
        """
        return s.count('A') < 2 and 'LLL' not in s

557 Reverse Words in a String III

class Solution(object):
    def reverseWords(self, s):
        """
        :type s: str
        :rtype: str
        """
        return ' '.join(map(lambda t:t[::-1], s.split(' ')))

572 Subtree of Another Tree

class Solution(object):
    def isSubtree(self, s, t):
        """
        :type s: TreeNode
        :type t: TreeNode
        :rtype: bool
        """
        if not s:
            return False
        if self.isSame(s, t):
            return True
        return self.isSubtree(s.left, t) or self.isSubtree(s.right, t)

    def isSame(self, s, t):
        if not s and not t:
            return True
        if not s or not t:
            return False
        if s.val != t.val:
            return False
        return self.isSame(s.left, t.left) and self.isSame(s.right, t.right)

593 Valid Square

class Solution(object):
    def validSquare(self, p1, p2, p3, p4):
        """
        :type p1: List[int]
        :type p2: List[int]
        :type p3: List[int]
        :type p4: List[int]
        :rtype: bool
        """
        a = (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2
        b = (p1[0] - p3[0]) ** 2 + (p1[1] - p3[1]) ** 2
        c = (p1[0] - p4[0]) ** 2 + (p1[1] - p4[1]) ** 2
        d = (p2[0] - p3[0]) ** 2 + (p3[1] - p2[1]) ** 2
        e = (p4[0] - p2[0]) ** 2 + (p4[1] - p2[1]) ** 2
        f = (p3[0] - p4[0]) ** 2 + (p3[1] - p4[1]) ** 2
        counter = set([a, b, c, d, e, f])
        if len(counter) == 2:
            t = [a, b, c, d, e, f]
            m, s = counter
            u = t.count(m)
            v = t.count(s)
            return (u == 4 and 2 * m == s) or (v == 4 and 2 * s == m)
        return False