public int numWays(int n, int k) {
if(n == 0){
return 0;
}
if(n == 1){
return k;
}
if (n==2)
{
return k*k;
}
int diffColorCounts = k*(k-1);
int sameColorCounts = k;
for(int i=3; i<n; i++) {
int temp = diffColorCounts;
diffColorCounts = (diffColorCounts + sameColorCounts) * (k-1);
sameColorCounts = temp;
}
return diffColorCounts + sameColorCounts;
}
参考资料;
https://segmentfault.com/a/1190000005740990