type
status
date
slug
summary
tags
category
icon
password
Property
Jul 13, 2023 01:59 PM

Recursion Example

 
notion image

Recursion (Review)

notion image
notion image

迭代和递归

迭代是依次循环,直到(不)满足条件,或者说是达到临界条件
递归是一种自上而下的思维方式,bottom-up approach
 
notion image

Sum Digits

notion image
notion image
 

Order of RecurSion Calls

notion image
 
notion image
notion image
 
notion image
需要执行到if n<10 满足之后,再结束一个函数的调用,再“归”回去执行后面的那个print函数
notion image

Two Implementations 两种实现

notion image
总结
  • 两种实现一样清晰,通常简洁的更好
  • 在这种情况下,更长的实现会更清晰
  • 再学习递归的时候,首先写基本列子(base case)

Fibonacci

notion image
notion image
notion image
notion image
notion image

Fibonacci Call Tree

notion image

Broken Fibonacci

notion image
Error!
 
notion image

Counting Partitions

notion image
notion image
notion image
notion image
notion image

Call Tree

notion image
notion image
这种情况下,我已经有了这种分配方式,所以这种的数量是1
notion image
notion image
notion image

Code

 
CS 61A(Part-8) - Sequences & Data AbstractionCS 61A(Part 6) - Recursion
  • Waline
Kitety
Kitety
独特为佳
公告
我曾经七次鄙视自己的灵魂
--卡里·纪伯伦
第一次,当它本可进取时,却故作谦卑;
第二次,当它在空虚时,用爱欲来填充;
第三次,在困难和容易之间,它选择了容易;
第四次,它犯了错,却借由别人也会犯错来宽慰自己;
第五次,它自由软弱,却把它认为是生命的坚韧;
第六次,当它鄙夷一张丑恶的嘴脸时,却不知那正是自己面具中的一副;
第七次,它侧身于生活的污泥中,虽不甘心,却又畏首畏尾。
 
最新评论
Loading...