★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-nyrfpmcv-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
We have two types of tiles: a 2x1 domino shape, and an "L" tromino shape. These shapes may be rotated.git
XX <- domino XX <- "L" tromino X
Given N, how many ways are there to tile a 2 x N board? Return your answer modulo 10^9 + 7.github
(In a tiling, every square must be covered by a tile. Two tilings are different if and only if there are two 4-directionally adjacent cells on the board such that exactly one of the tilings has both squares occupied by a tile.)微信
Example: Input: 3 Output: 5 Explanation: The five different ways are listed below, different letters indicates different tiles: XYZ XXZ XYY XXY XYY XYZ YYZ XZZ XYY XXY
Note:dom
[1, 1000]
.有兩種形狀的瓷磚:一種是 2x1 的多米諾形,另外一種是形如 "L" 的托米諾形。兩種形狀均可以旋轉。spa
XX <- 多米諾 XX <- "L" 托米諾 X
給定 N 的值,有多少種方法能夠平鋪 2 x N 的面板?返回值 mod 10^9 + 7。code
(平鋪指的是每一個正方形都必須有瓷磚覆蓋。兩個平鋪不一樣,當且僅當面板上有四個方向上的相鄰單元中的兩個,使得剛好有一個平鋪有一個瓷磚佔據兩個正方形。)htm
示例: 輸入: 3 輸出: 5 解釋: 下面列出了五種不一樣的方法,不一樣字母表明不一樣瓷磚: XYZ XXZ XYY XXY XYY XYZ YYZ XZZ XYY XXY
提示:blog
[1, 1000]
1 class Solution { 2 func numTilings(_ N: Int) -> Int { 3 switch N 4 { 5 case 0: 6 return 1 7 case 1,2: 8 return N 9 default: 10 break 11 } 12 var M:Int = Int(1e9) + 7 13 var dp:[Int] = [Int](repeating:0,count:N + 1) 14 dp[0] = 1 15 dp[1] = 1 16 dp[2] = 2 17 for i in 3...N 18 { 19 dp[i] = (dp[i - 1] * 2 + dp[i - 3]) % M 20 } 21 return dp[N] 22 } 23 }
4msget
1 class Solution { 2 func numTilings(_ N: Int) -> Int { 3 let MOD = 1000000007 4 if N == 1 { return 1 } 5 if N == 2 { return 2 } 6 var dp = [Int](repeating: 0, count: N+1) 7 dp[0] = 1; dp[1] = 1; dp[2] = 2 8 for i in 3...N { 9 dp[i] = (2*dp[i-1] + dp[i-3])%MOD 10 } 11 return dp[N] 12 } 13 }