diff --git "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" index 1d17784d43..d8e4e1d7f8 100644 --- "a/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" +++ "b/problems/0509.\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260.md" @@ -245,7 +245,7 @@ var fib = function(n) { }; ``` -TypeScript +### TypeScript ```typescript function fib(n: number): number { @@ -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) + } +} +``` + -----------------------