LeetCode1220. 统计元音字母序列的数目

相关信息:

LeetCode链接:
https://leetcode-cn.com/problems/count-vowels-permutation/

代码:

//作者:LeetCode-Solution
public class L1220 {
    public static void main(String[] args) {
    	//Test
        SolutionL1220 solutionL1220 = new SolutionL1220();
        int ans = solutionL1220.countVowelPermutation(3);
        System.out.println(ans);
    }
}

class SolutionL1220 {
    public int countVowelPermutation(int n) {
        // modvalue is set to limit the size of the returned value in subsequent mod operations to [0, value]
        long modValue = 1000000007;
        long[] dp = new long[5];
        long[] tempdp = new long[5];

        /*initializate dp[i]
        * i express 0:a 1:e 2:u 3:o 4:i
        * the value of dp[i] set to 1 to indicate the value passed in n = 1 situation
        * */
        for (int i = 0; i < 5; ++i) {
            //
            dp[i] = 1;
        }

        for (int i = 2; i <= n; ++i) {
            /* a前面可以为e,u,i */
            tempdp[0] = (dp[1] + dp[2] + dp[4]) % modValue;
            /* e前面可以为a,i */
            tempdp[1] = (dp[0] + dp[2]) % modValue;
            /* i前面可以为e,o */
            tempdp[2] = (dp[1] + dp[3]) % modValue;
            /* o前面可以为i */
            tempdp[3] = dp[2];
            /* u前面可以为i,o */
            tempdp[4] = (dp[2] + dp[3]) % modValue;
            /*Copy five consecutive values from the position where the index of tempdp array is 0,
            and put them into the dp array with the starting index of 0 in turn*/
            System.arraycopy(tempdp, 0, dp, 0, 5);
        }

        long ans = 0;
        /*add up the value of dp array
        * mod operation is to limit the numerical size of variable ans
        * */
        for (int i = 0; i < 5; ++i) {
            ans = (ans + dp[i]) % modValue;
        }
        return (int)ans;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值