美文网首页
5383. Number of Ways to Paint N

5383. Number of Ways to Paint N

作者: 7ccc099f4608 | 来源:发表于2020-04-12 23:15 被阅读0次

    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);
        }

    相关文章

      网友评论

          本文标题:5383. Number of Ways to Paint N

          本文链接:https://www.haomeiwen.com/subject/nonimhtx.html