算法
t0tott
timj3ly.com
展开
-
层序遍历
#include <iostream> #include <queue> using namespace std; struct TreeNode{ int val; TreeNode *left; TreeNode *right; TreeNode(int x): val(x), left(nullptr), right(nullptr){} }; void levelOrder(TreeNode *root){ if(root==nullp原创 2020-11-15 01:42:58 · 79 阅读 · 0 评论 -
ReverseList
#include <iostream> using namespace std; struct ListNode{ int val; struct ListNode *next; ListNode(int x): val(x), next(nullptr){} }; ListNode* ReverseList(ListNode *pHead){ if(!pHead) return nullptr; ListNode *fakeEnd = n原创 2020-10-29 19:15:31 · 99 阅读 · 0 评论 -
斐波纳切数列
题目 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0,第1项是1)。 n<=39 示例1 输入 4 返回值 3 代码 #include <iostream> using namespace std; int fibonacci(int n){ if(n == 1 || n == 0) return n; int first = 0, second = 1, sum = 0; for(int i=2; i<=n原创 2020-10-28 22:03:55 · 736 阅读 · 0 评论