Skip to content

Commit

Permalink
Fix: recursion bug for JS and TS (#1028)
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-tse committed Jan 6, 2024
1 parent edf3f3e commit 0f5b924
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ function forLoopRecur(n) {
const stack = [];
let res = 0;
// 递:递归调用
for (let i = 1; i <= n; i++) {
for (let i = n; i > 0; i--) {
// 通过“入栈操作”模拟“递”
stack.push(i);
}
// 归:返回结果
while (stack.length) {
while (stack.length) {
// 通过“出栈操作”模拟“归”
res += stack.pop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ function forLoopRecur(n: number): number {
const stack: number[] = [];
let res: number = 0;
// 递:递归调用
for (let i = 1; i <= n; i++) {
for (let i = n; i > 0; i--) {
// 通过“入栈操作”模拟“递”
stack.push(i);
}
// 归:返回结果
while (stack.length) {
while (stack.length) {
// 通过“出栈操作”模拟“归”
res += stack.pop();
}
Expand Down Expand Up @@ -67,4 +67,4 @@ console.log(`尾递归函数的求和结果 res = ${res}`);
res = fib(n);
console.log(`斐波那契数列的第 ${n} 项为 ${res}`);

export {};
export { };

0 comments on commit 0f5b924

Please sign in to comment.