在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given an integer
Since the answer may be too large, return it modulo
Example 1: Input: n = 1 Output: 5 Explanation: All possible strings are: "a", "e", "i" , "o" and "u". Example 2: Input: n = 2 Output: 10 Explanation: All possible strings are: "ae", "ea", "ei", "ia", "ie", "io", "iu", "oi", "ou" and "ua". Example 3: Input: n = 5 Output: 68
Constraints:
给你一个整数
由于答案可能会很大,所以请你返回 模
示例 1: 输入:n = 1 输出:5 解释:所有可能的字符串分别是:"a", "e", "i" , "o" 和 "u"。 示例 2: 输入:n = 2 输出:10 解释:所有可能的字符串分别是:"ae", "ea", "ei", "ia", "ie", "io", "iu", "oi", "ou" 和 "ua"。 示例 3: 输入:n = 5 输出:68
提示:
Runtime: 100 ms
Memory Usage: 21.2 MB
1 class Solution { 2 func countVowelPermutation(_ n: Int) -> Int { 3 var dp:[[Int]] = [[Int]](repeating: [Int](repeating: 0, count: 5), count: n + 1) 4 let MOD:Int = 1000000007 5 for i in 0..<5 6 { 7 dp[1][i] = 1 8 } 9 for i in 1..<n 10 { 11 dp[i+1][0] = (dp[i][1] + dp[i][2] + dp[i][4]) % MOD 12 dp[i+1][1] = (dp[i][0] + dp[i][2]) % MOD 13 dp[i+1][2] = (dp[i][1] + dp[i][3]) % MOD 14 dp[i+1][3] = dp[i][2] 15 dp[i+1][4] = (dp[i][2] + dp[i][3]) % MOD 16 } 17 var res:Int = 0 18 for i in 0..<5 19 { 20 res = (res + dp[n][i]) % MOD 21 } 22 return res 23 } 24 }
|
请发表评论