4 lines Python by math


  • 0

    Basically a math problem from wikipedia here.

    class Solution(object):
        def findDerangement(self, n):
            """
            :type n: int
            :rtype: int
            """
            ans, a, b = 1, 1, 0
            for i in range(2, n):
                ans, a, b = i * (a+b) % 1000000007, i * (a+b) % 1000000007, a
            return ans if n > 1 else 0
    

Log in to reply
 

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