博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Swift]LeetCode276. 粉刷栅栏 $ Paint Fence
阅读量:4511 次
发布时间:2019-06-08

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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝()
➤GitHub地址:
➤原文地址:  
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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.


有一个有N根柱子的栅栏,每根柱子都可以涂上一种K颜色。 

你必须把所有的柱子都刷上油漆,这样两个相邻的栅栏柱子就不会有相同的颜色。 

返回可以绘制围栏的方法总数。 

注:

n和k是非负整数。


Solution:

1 class Solution { 2     func numWays(_ n:Int,_ k:Int) -> Int { 3         if n == 0 {
return 0} 4 var same:Int = 0 5 var diff:Int = k 6 for i in 2...n 7 { 8 var t:Int = diff 9 diff = (same + diff) * (k - 1)10 same = t11 }12 return same + diff13 }14 }

 

转载于:https://www.cnblogs.com/strengthen/p/10685740.html

你可能感兴趣的文章
【模板】高精度
查看>>
弱弱的玩下Javascript
查看>>
二叉树相关操作
查看>>
在webstorm开发微信小程序之使用阿里自定义字体图标
查看>>
序列化模块/模块/包
查看>>
eclipse maven plugin 插件 安装 和 配置
查看>>
收集一些复杂有用的正则表达式
查看>>
子数组求和之大数溢出
查看>>
浏览器预览office文件(word,Excel,等)
查看>>
MySQL工具汇总
查看>>
cookie
查看>>
如何使用Eclipse编译C,C++,JAVA程序
查看>>
php小程序-文章发布系统
查看>>
使用md5加密的登录密码
查看>>
Java 执行jar linux 实例
查看>>
染色(bzoj 2243)
查看>>
理解托管磁盘的原理与优势
查看>>
[原创]python之简单计算器(超详解,只有基本功能+-*/,还有括号处理)
查看>>
python 多进程简单调用
查看>>
APP压力稳定性测试
查看>>