youngyangyang04 / leetcode-master-comment

用来做评论区
0 stars 0 forks source link

[Vssue]0070.爬楼梯.md #184

Open youngyangyang04 opened 5 months ago

youngyangyang04 commented 5 months ago

https://www.programmercarl.com/0070.%E7%88%AC%E6%A5%BC%E6%A2%AF.html

Du1in9 commented 5 months ago
int[] dp = new int[n + 1];
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i < n + 1; i++) {
    dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
// 例: n = 5, dp = [1,1,0,0,0,0]
i = 2: dp = [1,1,2,0,0,0]
i = 3: dp = [1,1,2,3,0,0]
i = 4: dp = [1,1,2,3,5,0]
i = 5: dp = [1,1,2,3,5,8]
JIE-yx commented 2 months ago

所以如果面试的时候,被问到dp[0]为什么初始化为1,应该怎么解释呢?从答案倒退初始化的值?

djsjznsn commented 1 month ago

@JIE-yx

所以如果面试的时候,被问到dp[0]为什么初始化为1,应该怎么解释呢?从答案倒退初始化的值?

你怎么想的,就怎么说

Rumiple commented 3 weeks ago

dp[i]表示的是到达第i个台阶的方法数,因此无论到达哪个台阶,无论咋动,方法数都至少为1,毕竟dp[i]=0的含义应该是无法到达这个台阶。所以dp[0]=1。