5383. Number of Ways to Paint N

2020-04-12  本文已影响0人  7ccc099f4608

https://leetcode-cn.com/problems/number-of-ways-to-paint-n-x-3-grid/submissions/

代码参考 & 解释,引用自:
https://leetcode-cn.com/problems/number-of-ways-to-paint-n-x-3-grid/solution/shu-xue-jie-jue-fei-chang-kuai-le-by-lindsaywong/

image.png

(图片来源https://leetcode-cn.com/problems/number-of-ways-to-paint-n-x-3-grid/submissions/

日期 是否一次通过 comment
2020-03-23 0

/** 遍历 */
public int numOfWays(int n) {
        if (n == 0){
            return 0;
        } else if (n == 1) {
            return 12;
        }

        int temp = 1000000007;
        long  repeat = 6;
        long  unrepeat = 6;
        for(int i = 2; i <=n; i++)
        {
            long  newrep = (repeat * 3) % temp + unrepeat * 2 % temp;
            long  newunrep = repeat * 2 % temp + unrepeat * 2 % temp;
            repeat = newrep;
            unrepeat = newunrep;
        }
        return (int)((repeat + unrepeat)%temp);
    }
上一篇下一篇

猜你喜欢

热点阅读