You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class Solution {
public:
int numWays(int n, int k) {
if (n == 0) return 0;
int same = 0, diff = k;
for (int i = 2; i <= n; ++i) {
int t = diff;
diff = (same + diff) * (k - 1);
same = t;
}
return same + diff;
}
};
下面这种解法和上面那方法几乎一样,只不过多了一个变量,参见代码如下:
解法二:
class Solution {
public:
int numWays(int n, int k) {
if (n == 0) return 0;
int same = 0, diff = k, res = same + diff;
for (int i = 2; i <= n; ++i) {
same = diff;
diff = res * (k - 1);
res = same + diff;
}
return res;
}
};
Activity