Skip to content

Commit

Permalink
添加 0509.斐波那契数.md Scala版本
Browse files Browse the repository at this point in the history
  • Loading branch information
wzqwtt committed Jun 18, 2022
1 parent fe3aa4f commit cf0affd
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion problems/0509.斐波那契数.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ var fib = function(n) {
};
```

TypeScript
### TypeScript

```typescript
function fib(n: number): number {
Expand Down Expand Up @@ -324,5 +324,33 @@ pub fn fib(n: i32) -> i32 {
return fib(n - 1) + fib(n - 2);
}
```

### Scala

动态规划:
```scala
object Solution {
def fib(n: Int): Int = {
if (n <= 1) return n
var dp = new Array[Int](n + 1)
dp(1) = 1
for (i <- 2 to n) {
dp(i) = dp(i - 1) + dp(i - 2)
}
dp(n)
}
}
```

递归:
```scala
object Solution {
def fib(n: Int): Int = {
if (n <= 1) return n
fib(n - 1) + fib(n - 2)
}
}
```

-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 comments on commit cf0affd

Please sign in to comment.