摘要: 题目 剑指 Offer 07. 重建二叉树 解析 确定中序遍历中头节点位置index 计算左子树长度size = index - inl,inl为中序遍历的起点 构建左子树 前序遍历位置:[prel + 1, prel + size] 中序遍历位置:[inl, index - 1] 构建右子树 前序 阅读全文
posted @ 2022-04-17 20:56 当惜 阅读(16) 评论(0) 推荐(0)
摘要: 题目 剑指 Offer 06. 从尾到头打印链表 代码 递归 class Solution { public: vector<int> reversePrint(ListNode* head) { if(!head) return {}; auto res = reversePrint(head-> 阅读全文
posted @ 2022-04-17 16:36 当惜 阅读(15) 评论(0) 推荐(0)
摘要: 题目 剑指 Offer 05. 替换空格 代码 class Solution { public: string replaceSpace(string s) { string res; for(auto x : s) { if(x == ' ') res += "%20"; else res += 阅读全文
posted @ 2022-04-17 16:21 当惜 阅读(17) 评论(0) 推荐(0)
摘要: 题目 剑指 Offer 04. 二维数组中的查找 代码 class Solution { public: bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) { if(matrix.empty()) return fal 阅读全文
posted @ 2022-04-17 16:12 当惜 阅读(17) 评论(0) 推荐(0)
摘要: 题目 剑指 Offer 03. 数组中重复的数字 代码 class Solution { public: int findRepeatNumber(vector<int>& nums) { for(int i = 0; i < nums.size(); i++) { while(nums[nums[ 阅读全文
posted @ 2022-04-17 11:25 当惜 阅读(18) 评论(0) 推荐(0)