题目挺简单的,就是溢出、取余特别令人抓狂
class Solution { public: const int MOD = 1'000'000'007; int count(const int &choices,const int &num){ if(num <= 2) return num; if(num == 3) return 4; vector<long> dp(num+1);//dp[i]表示 i 个字符的组合个数 dp[0] = 1;dp[1] = 1;dp[2] = 2;dp[3] = 4; for(int i = 4;i <= num;++i){ dp[i] += (dp[i-1] + dp[i-2] + dp[i-3]); if(choices == 4) dp[i] += dp[i-4]; if(dp[i] >= MOD) dp[i] %= MOD; } return dp[num]; } int countTexts(string pressedKeys) { int n = pressedKeys.size(); long res = 1; for(int i = 0,j = 0;i < n && j < n;){ while(j < n && pressedKeys[i] == pressedKeys[j]) ++j; if(pressedKeys[i] == '7' || pressedKeys[i] == '9'){ res = res * count(4,j-i) % MOD;//如果写成res *= count(4,j-i)就会溢出 } else res = res * count(3,j-i) % MOD; i = j; } if(res >= MOD) return res & MOD; return res; } };
标签:int,res,打字,num,pressedKeys,2266,leetcode,dp,MOD From: https://www.cnblogs.com/uacs2024/p/18638705