随笔分类 -  leetcode做题

摘要:输入数据格式:(完全二叉树) arr = [1,2,3,4,5,null,6] 主函数: var tree = function(arr){ let root = construct(arr); // 构造二叉树 let ans = preorderTraversal(root); //前序遍历-迭 阅读全文
posted @ 2020-07-20 15:21 _Sleeping 阅读(206) 评论(0) 推荐(0)
摘要:题目描述 leetcode - 19:https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/ 解题关键 链表 碎碎念 中等题因为要求是 一次 循环。所以用两个TreeNode节点tmp和ntmp,ntmp = tmp - 阅读全文
posted @ 2020-06-18 23:27 _Sleeping 阅读(110) 评论(0) 推荐(0)
摘要:题目描述 leetcode - 102:https://leetcode-cn.com/problems/binary-tree-level-order-traversal/ 解题关键 队列 BFS 结构体 碎碎念 这道题可以不用结构体,在while循环里面加一个for循环来遍历某一层的节点。但是很 阅读全文
posted @ 2020-06-18 23:18 _Sleeping 阅读(182) 评论(0) 推荐(0)
摘要:题目描述 leetcode - 206:https://leetcode-cn.com/problems/reverse-linked-list/ 解题关键 链表 递归 代码 /** * Definition for singly-linked list. * struct ListNode { * 阅读全文
posted @ 2020-06-16 18:59 _Sleeping 阅读(125) 评论(0) 推荐(0)
摘要:题目描述 leetcode - 15:https://leetcode-cn.com/problems/3sum/ 解题关键 双指针(把 a+b+c=0 化成 求 -a=b+c ) 代码 vector<vector<int>> threeSum(vector<int>& nums) { vector 阅读全文
posted @ 2020-06-16 00:18 _Sleeping 阅读(91) 评论(0) 推荐(0)
摘要:题目描述 leetcode - 17:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/submissions/ 解题关键 递归 回溯 碎碎念 感觉就是一个递归加回溯。 把digits列出的数字键按顺序递归d 阅读全文
posted @ 2020-06-08 18:18 _Sleeping 阅读(170) 评论(0) 推荐(0)
摘要:题目描述 leetcode - 22:https://leetcode-cn.com/problems/container-with-most-water/ 解题关键 深搜 回溯 碎碎念 感觉自己一直对dfs这东西有点迷,不太会写,这次主要参考了大佬的答案。希望自己多写写有一天能灵光一现彻底搞懂了这 阅读全文
posted @ 2020-06-06 00:05 _Sleeping 阅读(77) 评论(0) 推荐(0)
摘要:题目描述 leetcode - 994:https://leetcode-cn.com/problems/container-with-most-water/ 解题关键 宽搜 碎碎念 这题除了用 宽搜 以外,还用了 结构体 来存数据,因为需要记录搜索中的层数。宽搜得到的树的 高度 其实就是答案。 用 阅读全文
posted @ 2020-06-02 17:41 _Sleeping 阅读(167) 评论(0) 推荐(0)
摘要:题目描述 leetcode - 11:https://leetcode-cn.com/problems/container-with-most-water/ 解题关键 双指针法 求出当前双指针对应的容器的容量 对应数字较小的那个指针以后不可能作为容器的边界了,将其丢弃,并移动对应的指针。 碎碎念 双 阅读全文
posted @ 2020-06-02 02:01 _Sleeping 阅读(103) 评论(0) 推荐(0)
摘要:题目描述 leetcode - 1:https://leetcode-cn.com/problems/two-sum/ 解题关键 hashmap的使用 碎碎念 题目比较简单,暴力过很容易,不过借助hash可以降低时间复杂度,但是增加了空间的消耗。学习了hashmap的使用 key:value 定义 阅读全文
posted @ 2020-05-31 23:22 _Sleeping 阅读(103) 评论(0) 推荐(0)
摘要:题目描述 leetcode - 5:https://leetcode-cn.com/problems/longest-palindromic-substring/submissions/ 解题关键 动态规划 中心扩散(比较好理解) 代码 1、暴力(会超时) 时间复杂度O(N^3),N 是字符串的长度 阅读全文
posted @ 2020-05-31 01:11 _Sleeping 阅读(295) 评论(0) 推荐(0)
摘要:题目描述 leetcode - 2:https://leetcode-cn.com/problems/add-two-numbers/ 解题关键 C++ 链表 数据结构的定义和遍历 代码 /** * Definition for singly-linked list. * struct ListNo 阅读全文
posted @ 2020-05-29 02:23 _Sleeping 阅读(140) 评论(0) 推荐(0)
摘要:题目描述 leetcode - 3:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 解题关键 滑动窗口 C++的hashset的使用 代码 class Solution { public 阅读全文
posted @ 2020-05-26 20:47 _Sleeping 阅读(142) 评论(0) 推荐(0)
摘要:题目描述 leetcode - 912:https://leetcode-cn.com/problems/sort-an-array/ 解题关键 快排 冒泡 归并 代码 class Solution { // 快速排序 int partition(vector<int>& nums,int l,in 阅读全文
posted @ 2020-05-21 17:49 _Sleeping 阅读(115) 评论(0) 推荐(0)