力扣-1641. 统计字典序元音字符串的数目

Idea

用排列枚举的思想
当 n = 1 时,全部为1
当 n = 2 时,a 的种数是 n = 1 五个全部加起来的种数,e 的种数是 后四个全部加起来的种数,i 的种数是后三个全部加起来的种数

以此类推

AC Code

class Solution {
public:
    int countVowelStrings(int n) {
        if(n == 1) return 5;
        vector<int> a(5,1);
        int ans = 0;
        for(int i=2;i<=n;i++){
            ans = 0;
            for(int j=0;j<5;j++){
                for(int k=j+1;k<5;k++){
                    a[j]+=a[k];
                }
            }
            for(int j=0;j<5;j++) ans+=a[j];
        }
        return ans;
    }
};

你可能感兴趣的:(LeetCode,leetcode,算法)