[Lc]155最小栈 2020-05-18 leetcode 题目 题解 两个类似的方法,一个是使用辅助栈存当前的最小值,一个是直接用一个栈保存一个pair,包括原值和最小值,个人感觉后一种方法更加直观,即 Read more...
[Lc]54螺旋矩阵 2020-05-17 leetcode 题目 题解 就是分四种情况,从左到右,上到下,右到左,下到上循环遍历,和59题类似 时间复杂度$O(nm)$ 空间复杂度$O(nm)$ class Solution { public: vector<int> spiralOrder(vector<vector<int>>& matrix) Read more...
[Lc]59螺旋矩阵II 2020-05-17 leetcode 题目 题解 就是分四种情况,从左到右,上到下,右到左,下到上循环遍历,和54题类似 时间复杂度$O(nm)$ 空间复杂度$O(nm)$ class Solution { public: vector<vector<int>> generateMatrix(int n) Read more...
[Lc]面试题29顺时针打印矩阵 2020-05-17 剑指offer 题目 题解 直接迭代,四个方向将结果输入res,注意边界条件的判断 时间复杂度$O(n)$ 空间复杂度$O(n)$ class Solution { public: vector<int> spiralOrder(vector<vector<int>>& matrix) { vector<int> res; if(matrix.empty()) return res; int u=0, d=matrix.size()-1, l=0, Read more...
[Lc]面试题28对称的二叉树 2020-05-17 剑指offer 题目 题解 二叉树结构如下: //Definition for a binary tree node. struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; 这道题三种解法,递归,栈迭代(先序遍历),队列迭代(层次遍历)。与10 Read more...
[Lc]面试题27二叉树的镜像 2020-05-16 剑指offer 题目 题解 二叉树结构如下: //Definition for a binary tree node. struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; 1. 递归法 这个方法就是递归的翻转左右子树,若遇到空子树直接返回,然后当前 Read more...
[Lc]面试题26树的子结构 2020-05-15 剑指offer 题目 题解 二叉树结构如下: //Definition for a binary tree node. struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; 1. 双递归法 两个递归函数,一个用来看当前节点是否匹配,一个用来遍历a的所 Read more...
[Lc]面试题25合并两个排序的链表 2020-05-15 剑指offer 题目 链表定义: //Definition for singly-linked list. struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; 题解 这道题的思想是链表归并排序中的一个重要环节,即对有序的两个链表进行归并。比较表头,小的 Read more...
[Lc]21合并两个有序链表 2020-05-15 leetcode 题目 链表定义: //Definition for singly-linked list. struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; 题解 这道题的思想是链表归并排序中的一个重要环节,即对有序的两个链表进行归并。比较表头,小的 Read more...
[Lc]206反转链表 2020-05-15 剑指offer 题目 链表定义: //Definition for singly-linked list. struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; 题解 1. 迭代法(双指针) 实际上是三个指针,一个指向当前节点之前pre,一个指向当前节点cur Read more...