Skip to content

Instantly share code, notes, and snippets.

@wangdu1005
Created June 12, 2019 01:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wangdu1005/dbfbd0500d8a1b756d05d69eb5f74a53 to your computer and use it in GitHub Desktop.
Save wangdu1005/dbfbd0500d8a1b756d05d69eb5f74a53 to your computer and use it in GitHub Desktop.
LeetCode
class Solution {
func fib(_ N: Int) -> Int {
return self.recursiveWay(N)
}
func recursiveWay(_ N: Int) -> Int {
if N == 0 { return 0 }
if N == 1 { return 1 }
var memo = [Int](repeating: 0, count: 3)
memo[0] = 0
memo[1] = 1
for _ in 2...N {
memo[2] = memo[1] + memo[0]
memo[0] = memo[1]
memo[1] = memo[2]
}
return memo[2]
}
func iterationWay(_ N: Int) -> Int {
if N == 0 { return 0 }
if N == 1 { return 1 }
return self.iterationWay(N-1) + self.iterationWay(N-2)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment