diff --git a/CodingInterview.md b/CodingInterview.md new file mode 100644 index 0000000..7532346 --- /dev/null +++ b/CodingInterview.md @@ -0,0 +1,15 @@ +# 《剑指offer》第2版对应LeetCode题目 + +《剑指offer》第2版中的面试题对应的LeetCode题目。 +* 编号:题目在书中的编号; +* 题目:[《牛客网剑指offer专题》](https://www.nowcoder.com/ta/coding-interviews?page=1)对应题目链接; +* LeetCode: 对应LeetCode题目的题解链接; + + +| 编号 | 题目 | LeetCode | 备注 | +| ---- | --- | ----- | ----- | ---- | +| 3 | [数组中重复的数字](https://www.nowcoder.com/practice/623a5ac0ea5b4e5f95552655361ae0a8?tpId=13&tqId=11203&tPage=3&rp=3&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking) | [287. Find the Duplicate Number](solutions/287.%20Find%20the%20Duplicate%20Number.md)| 注意题目之间细微差别 | +| 4 | [二维数组中的查找](https://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e?tpId=13&tqId=11154&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking) | [240. Search a 2D Matrix II](solutions/240.%20Search%20a%202D%20Matrix%20II.md)| +| 7 | [重建二叉树](https://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6?tpId=13&tqId=11157&tPage=1&rp=3&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking) | [105. Construct Binary Tree from Preorder and Inorder Traversal](solutions/105.%20Construct%20Binary%20Tree%20from%20Preorder%20and%20Inorder%20Traversal.md)| +| 9 | [用两个栈实现队列](https://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6?tpId=13&tqId=11158&tPage=1&rp=3&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking) | [232. Implement Queue using Stacks](solutions/232.%20Implement%20Queue%20using%20Stacks.md)| + diff --git a/README.md b/README.md index 62405dd..f05ec0c 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,9 @@ My LeetCode solutions with Chinese explanation. 我的LeetCode中文题解。 每个题目都保证击败超过80%的提交,并尽量给出多种解法并分析算法复杂度。 每日持续更新中,欢迎交流,欢迎star :D -另外,[`algorithm`](algorithm/README.md)文件夹对经典算法作了简单介绍并给出代码模板,方便刷题参考使用。 +另外, +* [algorithm](algorithm/README.md)文件夹对经典算法作了简单介绍并给出代码模板,方便刷题参考使用; +* [CodingInterview](CodingInterview.md)给出了《剑指offer》第2版中对应的LeetCode题目。 | # | 题目 | 题解 | 难度 | | ---- | -----------| ------- | ------- | diff --git a/solutions/509. Fibonacci Number.md b/solutions/509. Fibonacci Number.md new file mode 100644 index 0000000..cfd760a --- /dev/null +++ b/solutions/509. Fibonacci Number.md @@ -0,0 +1,49 @@ +# [509. Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) + +# 思路 +求斐波那契数列。 + +## 思路一 +常用思路就是按照定义迭代计算,很简单,见代码。 + +时间复杂度O(N),空间复杂度O(1)。 + +## 思路二 +除了基本的思路,还有个O(logN)复杂度求斐波那契数列的思路。 + +根据定义,我们有 +``` +| F(N) F(N-1)| | 1 1 |(N-1) +| | = | | +| F(N-1) F(N-2)| | 1 0 | +``` +以上公式不难用归纳法证明。所以要想得到F(N),只需要求得矩阵 +``` +|1 1| +|1 0| +``` +的N-1次方。如何求这个矩阵的乘方呢,如果简单地循环N-1次那么复杂度还是O(N)。而我们知道求乘方有一个O(logN)的二分算法: +* n为偶数:A^n = A^(n/2) * A^(n/2) +* n为奇数:A^n = A^(n/2) * A^(n/2) * A + +以上求乘方的方法可以很方便地用递归实现。 + +这种思路虽然时间复杂度为O(logN),但是隐含的时间常数比较大,所以不是很常用,这里代码略。但是这种用O(logN)的二分求乘方的思路是值得我们学习的。 + +# C++ +## 思路一 +``` C++ +class Solution { +public: + int fib(int N) { + if(N <= 1) return N; + int pre = 1, prepre = 0, tmp; + for(int i = 2; i <= N; i++){ + tmp = pre + prepre; + prepre = pre; + pre = tmp; + } + return pre; + } +}; +``` \ No newline at end of file