博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
276. Paint Fence
阅读量:4615 次
发布时间:2019-06-09

本文共 641 字,大约阅读时间需要 2 分钟。

There is a fence with n posts, each post can be painted with one of the k colors.

You have to paint all the posts such that no more than two adjacent fence posts have the same color.

Return the total number of ways you can paint the fence.

Note:

n and k are non-negative integers.

思路:最多两个相邻同色。之前不同色*(k-1)+之前同色*(k-1)就是现在不同色的个数。最后输出现在不同色加现在同色的总个数。

public class Solution {    public int numWays(int n, int k) {        if(n<=0||k<=0)        {            return 0;        }        if(n==1)        {            return k;        }        int same=k;        int diff=k*(k-1);        for(int i=2;i

 

转载于:https://www.cnblogs.com/Machelsky/p/5894847.html

你可能感兴趣的文章
Selenium 管理 Cookies
查看>>
exceptionfunction[LeetCode]Permutations
查看>>
bzoj 4595 激光发生器
查看>>
multi cookie & read bug
查看>>
js时间转换
查看>>
(转载) Android Studio你不知道的调试技巧
查看>>
队列实现霍夫曼树
查看>>
关于微信公众平台测试号配置失败的问题
查看>>
【NOIP2001】统计单词个数
查看>>
linux常用端口
查看>>
异常处理
查看>>
/proc/uptime详解
查看>>
如何建立合适的索引?
查看>>
acwing 651. 逛画展
查看>>
Vijos P1243 生产产品 (单调队列优化DP)
查看>>
iOS常用第三方库 -转
查看>>
Android布局学习
查看>>
jQuery中事件绑定与解绑
查看>>
js原生Ajax的封装与使用
查看>>
周总结6
查看>>