chapter_computational_complexity/iteration_and_recursion/ #694
Replies: 92 comments 124 replies
-
我算法小白理解的递归和尾递归,递归就好像是拿着个小本本,算一次,算不出答案,需要标记一下继续算,一直到最后一个答案算出来再根据标记往回总结答案,而尾递归则是算一次更新一下答案,所以到最后答案可以直接得出,这样的话感觉跟迭代很像啊 |
Beta Was this translation helpful? Give feedback.
-
简单查了一下好像支持尾递归优化的语言不多,相对主流一些的语言 Java、Python、Go 之类的都不支持。 |
Beta Was this translation helpful? Give feedback.
-
f(n),f是什么意思 |
Beta Was this translation helpful? Give feedback.
-
希望添加迭代器的代码 |
Beta Was this translation helpful? Give feedback.
-
我愿称之为最强教程。这一章终于完全理解了迭代和递归的不同,终于完全理解了尾递归。豁然开朗的爽感让我继续如饥似渴地阅读下面的内容! |
Beta Was this translation helpful? Give feedback.
-
很多语言没有对尾递归进行优化,数据量不确定的情况下,用递归风险岂不是很大 |
Beta Was this translation helpful? Give feedback.
-
开头第一段话因为我不是很懂,所以去问了一下GPT,回复说:“迭代和递归都是程序中常见的控制结构,而不是程序结构本身。”这个回答是正确的吗?以及控制结构和程序结构的同异是什么呢?🤔 |
Beta Was this translation helpful? Give feedback.
-
比作挖坑的话 然后迭代解题关键就是计算好每次挖多少,迭代每次挖的土都是固定的重量,最后只管算挖了几次就能知道。 迭代跟递归配合就是 在原地挖坑?不用往前走也不用去下一层 直接原地计算?有点迷糊了。
} |
Beta Was this translation helpful? Give feedback.
-
例如在以下代码中,条件变量,每轮进行了两次更新,这种情况就不太方便用 for 循环实现。 /* while 循环(两次更新) */
int whileLoopII(int n) {
int res = 0;
int i = 1; // 初始化条件变量
// 循环求和 1, 4, ...
while (i <= n) {
res += i;
// 更新条件变量
i++;
i *= 2;
}
return res;
} 这个地方提到用for循环不好实现,可是 int res = 0;
for (int i1 = 1; i1 <= n; ) {
res += i1;
i1++;
i1 *= 2;
} 这样写也一样能实现的,感觉和while没啥大的区别 |
Beta Was this translation helpful? Give feedback.
-
分治是一种算法设计思想,它通过将问题划分为多个相同或相似子问题,并分别解决这些子问题,最后将子问题的解合并起来得到原始问题的解。分治思想通常包括三个步骤:分解、解决和合并。
分治思想通常用于解决问题的规模较大、复杂度较高的情况,它可以将原问题划分为多个较小的子问题,从而简化原问题的求解过程。常见的应用包括归并排序、快速排序、二分搜索等。通过分治思想,可以提高算法的效率和可扩展性。分治是一种算法设计思想,它通过将问题划分为多个相同或相似子问题,并分别解决这些子问题,最后将子问题的解合并起来得到原始问题的解。分治思想通常包括三个步骤:分解、解决和合并。
分治思想通常用于解决问题的规模较大、复杂度较高的情况,它可以将原问题划分为多个较小的子问题,从而简化原问题的求解过程。常见的应用包括归并排序、快速排序、二分搜索等。通过分治思想,可以提高算法的效率和可扩展性。 |
Beta Was this translation helpful? Give feedback.
-
”普通递归:当函数返回到上一层级的函数后,需要继续执行代码,因此系统需要保存上一层调用的上下文。” |
Beta Was this translation helpful? Give feedback.
-
C#的命名有点不友好, |
Beta Was this translation helpful? Give feedback.
-
问在for循环中什么c++里面是++i,c中就是i++ |
Beta Was this translation helpful? Give feedback.
-
请问,我用 print(fib(4)); 运行得到的结果是2,这和理解上的不一致,在本本上演算也是2,请问是哪里做的不对吗? |
Beta Was this translation helpful? Give feedback.
-
尾递归:递归调用是函数返回前的最后一个操作(它的返回值不属于表达式的一部分)。个人感觉,加上括号里的话可能更好理解一点。文中的普通递归,在函数的递归调用和返回之间,还进行了一次加法操作,因此还需要给n分配空间。 |
Beta Was this translation helpful? Give feedback.
-
嵌套循环:注释:n * n 为对应点数量,"(i, j), " 对应字符串长最大为 6+102,加上最后一个空字符 \0 的额外空间 |
Beta Was this translation helpful? Give feedback.
-
个人小结:
|
Beta Was this translation helpful? Give feedback.
-
既然是斐波那契数列,那我就用
|
Beta Was this translation helpful? Give feedback.
-
感谢!第一次觉得尾递归,递归转换成迭代这么容易理解。 |
Beta Was this translation helpful? Give feedback.
-
力扣对应部分的算法题,递归和回溯部分,反向看答案到思路是能看懂的,但正向自己去推导,有时候能推导出中文思路,但是没法构想出递归方程式TAT |
Beta Was this translation helpful? Give feedback.
-
For(int i=0 ; i<n ; i=(i+1)*2 ){}这种写法也是成立的。for循环的第三个表达式可以看成是循环体内的最后一个语句。所以例子跟结论不怎么对应。但意思我理解了,也知道怎么用。 |
Beta Was this translation helpful? Give feedback.
-
尾递归为什么之前的函数不会被保留 |
Beta Was this translation helpful? Give feedback.
-
递归怎么通过栈转化成迭代的 |
Beta Was this translation helpful? Give feedback.
-
调用栈小节中说“递归调用函数会产生额外的开销。因此递归通常比循环的时间效率更低。”,请问递归调用函数会产生额外的开销指的是什么? |
Beta Was this translation helpful? Give feedback.
-
循环Fib |
Beta Was this translation helpful? Give feedback.
-
感觉对尾递归和普通递归的区别理解还是有点模糊 |
Beta Was this translation helpful? Give feedback.
-
尾递归是return 调用结果,普通递归是在方法体中调用,所以尾递归能够在编译器的优化下不用开辟临时空间
---- Replied Message ----
***@***.***>Date6/29/2024 ***@***.***>***@***.***>,
***@***.***>SubjectRe: [krahets/hello-algo] chapter_computational_complexity/iteration_and_recursion/ (Discussion #694)
感觉对尾递归和普通递归的区别理解还是有点模糊
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
chapter_computational_complexity/iteration_and_recursion/
动画图解、一键运行的数据结构与算法教程
https://www.hello-algo.com/chapter_computational_complexity/iteration_and_recursion/
Beta Was this translation helpful? Give feedback.
All reactions