★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-npawldzm-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given a positive integer n, return the number of all possible attendance records with length n, which will be regarded as rewardable. The answer may be very large, return it after mod 109 + 7.git
A student attendance record is a string that only contains the following three characters: github
A record is regarded as rewardable if it doesn't contain more than one 'A' (absent) or more than two continuous 'L' (late).微信
Example 1:spa
Input: n = 2 Output: 8 Explanation: There are 8 records with length 2 will be regarded as rewardable: "PP" , "AP", "PA", "LP", "PL", "AL", "LA", "LL" Only "AA" won't be regarded as rewardable owing to more than one absent times.
Note: The value of n won't exceed 100,000.code
給定一個正整數 n,返回長度爲 n 的全部可被視爲可獎勵的出勤記錄的數量。 答案可能很是大,你只需返回結果mod 109 + 7的值。htm
學生出勤記錄是隻包含如下三個字符的字符串:blog
若是記錄不包含多於一個'A'(缺勤)或超過兩個連續的'L'(遲到),則該記錄被視爲可獎勵的。three
示例 1:字符串
輸入: n = 2 輸出: 8 解釋: 有8個長度爲2的記錄將被視爲可獎勵: "PP" , "AP", "PA", "LP", "PL", "AL", "LA", "LL" 只有"AA"不會被視爲可獎勵,由於缺勤次數超過一次。
注意:n 的值不會超過100000。
1 class Solution { 2 func checkRecord(_ n: Int) -> Int { 3 var M:Int = 1000000007; 4 var P:[Int] = [Int](repeating:0,count:n + 1) 5 var PorL:[Int] = [Int](repeating:0,count:n + 1) 6 P[0] = 1 7 PorL[0] = 1 8 PorL[1] = 2 9 for i in 1...n 10 { 11 P[i] = PorL[i - 1] 12 if i > 1 13 { 14 PorL[i] = (P[i] + P[i - 1] + P[i - 2]) % M 15 } 16 } 17 var res:Int = PorL[n]; 18 for i in 0..<n 19 { 20 var t:Int = (PorL[i] * PorL[n - 1 - i]) % M 21 res = (res + t) % M 22 } 23 return res 24 } 25 }