
春招
chenchenxiaojian
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
leetcode-二叉搜索树中第K小的元素
题目https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/思路中序遍历实现中序遍历两种方法:1. 递归; 2:迭代code迭代// 迭代实现中序遍历var kthSmallest = function (root, k) { let stack = []; let res; while (root || stack.length) { if (root) {原创 2021-04-23 13:54:52 · 114 阅读 · 0 评论 -
leetcode-岛屿数量
题目https://leetcode-cn.com/problems/number-of-islands/思路遍历二维数组,若遇到一个陆地,res+1,但是为了不重复计算,将这个陆地周围跟他连通的并且也是陆地的值设置为’0’,也就是设置为水,这样就不会重复计算,设置为水的这个过程就是一个dfs过程,对陆地的四个方向进行dfs。code/** * @param {character[][]} grid * @return {number} */var numIslands = functio原创 2021-04-23 10:03:34 · 169 阅读 · 0 评论 -
leetcode-反转链表
题目https://leetcode-cn.com/problems/reverse-linked-list/思路递归:大问题拆成两个小问题,小问题和大问题有同样的解题步骤,存在最小子问题,本题中,在最小子问题就是只有一个节点的时候,也就是假如1->null就不必翻转,return 1出来即可,然后假设之前的连接为2->1->null,那么首先2->1->2,然后断开2->1,也就是2->1转化为2->null,所以就是成为1->2->nu原创 2021-04-20 16:08:58 · 98 阅读 · 0 评论 -
leetcode-二叉树的层序遍历
题目https://leetcode-cn.com/problems/binary-tree-level-order-traversal/思路队列,进入循环获取当前队列的节点个数,也就是上一层的节点个数,然后for循环对这些节点进行操作即可code/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val原创 2021-04-20 15:14:30 · 104 阅读 · 0 评论 -
leetcode-链表中倒数第k个节点
题目https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/思路快慢指针code/** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } *//** * @param {ListNode} head * @原创 2021-04-20 14:56:39 · 121 阅读 · 0 评论 -
leetcode-求根结点到叶节点数字之和
题目https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/思路dfscode/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left)原创 2021-04-20 14:43:17 · 104 阅读 · 0 评论 -
leetcode-长度最小的子数组
题目https://leetcode-cn.com/problems/minimum-size-subarray-sum/思路滑动窗口,首先向右扩大窗口,若此时窗口内的值大于target,那么可以收缩窗口,即是从左边压缩,找到最小值并记录code/** * @param {number} target * @param {number[]} nums * @return {number} */var minSubArrayLen = function(target, nums) {原创 2021-04-20 14:27:50 · 128 阅读 · 0 评论 -
leetcode-最大子序和
题目https://leetcode-cn.com/problems/maximum-subarray/思路dp贪心code/** * @param {number[]} nums * @return {number} */var maxSubArray = function (nums) { // return greedy(nums); return dp_f(nums);};const greedy = function (nums) { le原创 2021-04-20 10:21:43 · 103 阅读 · 0 评论 -
leetcode-路径总和
题目https://leetcode-cn.com/problems/path-sum/思路dfsbfs + 两个队列code/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left)原创 2021-04-19 16:39:40 · 112 阅读 · 0 评论 -
leetcode-二叉树的所有路径
题目https://leetcode-cn.com/problems/binary-tree-paths/思路dfs + 递归dfs + 栈code/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null :原创 2021-04-19 10:58:42 · 77 阅读 · 0 评论 -
leetcode-javascript-二叉树的最大深度
题目https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/思路dfsbfs+队列code/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? n原创 2021-04-19 09:48:01 · 121 阅读 · 0 评论 -
leetcode-全排列
题目https://leetcode-cn.com/problems/permutations/思路dfs + 回溯bfs + 队列codevar permute = function (nums) { // dfs // let depth = 0; // let tags = new Array(nums.length).fill(false); // let res, tmp; // res = []; // tmp = [];原创 2021-04-18 23:28:51 · 170 阅读 · 0 评论 -
leetcode-javascript-合并两个有序数组
题目https://leetcode-cn.com/problems/merge-sorted-array/思路用双指针,分别指向两个数组,比较然后push到一个新的数组中,最后在把新数组的值赋值给nums1反向双指针:对两个数组进行比较,较大值放在num1数组后面,就不用使用到O(m+n)的空间复杂度code/** * @param {number[]} nums1 * @param {number} m * @param {number[]} nums2 * @param {nu原创 2021-04-18 21:38:09 · 126 阅读 · 0 评论 -
leetcode-最长回文子串
题目https://leetcode-cn.com/problems/longest-palindromic-substring/思路中心扩散法:分为两种情况,…aa…和…aba…,用ll和rr记录这个子串的边界,判断长度rr-ll+1获取最大即可/** * @param {string} s * @return {string} */var longestPalindrome = function(s) { if (s.length == 0) return s; let原创 2021-04-18 21:05:32 · 79 阅读 · 0 评论 -
leetcode-重排链表
题目https://leetcode-cn.com/problems/reorder-list/submissions/思路快慢指针找到中间节点将链表根据中间节点分成两个,对后面这个链表进行倒序然后在依次组合这两个链表code/** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * this.原创 2021-04-14 16:49:01 · 140 阅读 · 0 评论 -
leetcode-最小的k个数
题目https://leetcode-cn.com/problems/zui-xiao-de-kge-shu-lcof/code快速排序var getLeastNumbers = function (arr, k) { if (k > arr.length) return []; heap_sort(arr, arr.length); return arr.slice(0, k);}最大堆/** * @param {number[]} arr * @para原创 2021-04-07 03:13:07 · 282 阅读 · 0 评论 -
leetcode-比较版本号
题目https://leetcode-cn.com/problems/compare-version-numbers/solution/思路找到每个.分割的版本号,然后转换成数字进行比较code/** * @param {string} version1 * @param {string} version2 * @return {number} */var compareVersion = function (version1, version2) { let len1 = ve原创 2021-04-01 14:12:42 · 144 阅读 · 0 评论 -
leetcode-顺时针打印矩阵
题目https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/思路按照每层打印,外层打完,进内层;先打印上侧,右侧若:left<right&&top<bottom,说明存在下侧,左侧需要打印code/** * @param {number[][]} matrix * @return {number[]} */var spiralOrder = function (matrix) {原创 2021-03-30 00:01:28 · 99 阅读 · 0 评论 -
leetcode-括号生成
题目https://leetcode-cn.com/problems/generate-parentheses/思路递归生成,当做一颗树,每层加入一个)或(,这样子就可以用dfs把所有的组合列出来,因为是有效的括号,对树的每一层进行判断,当右括号数大于左括号数,不成立;当左括号数大于总数的一半,不成立。code/** * @param {number} n * @return {string[]} */function f(n,res,str, left, right){ if (原创 2021-03-29 12:42:13 · 113 阅读 · 0 评论 -
leetcode-二分查找
题目https://leetcode-cn.com/problems/binary-search/code/** * @param {number[]} nums * @param {number} target * @return {number} */var search = function(nums, target) { let begin = 0; let end = nums.length; while (begin <= end){原创 2021-03-29 11:50:25 · 88 阅读 · 0 评论 -
leetcode-字符串相加
题目https://leetcode-cn.com/problems/add-strings/思路大数相加,模拟code/** * @param {string} num1 * @param {string} num2 * @return {string} */var addStrings = function(num1, num2) { let len1 = num1.length-1; let len2 = num2.length-1; let add = 0原创 2021-03-29 11:36:17 · 114 阅读 · 0 评论