fantc

Results 10 comments of fantc

``` js Promise.retry = function(fn, num){ return new Promise(function(resolve, reject){ while(num>0){ try{ const res = await fn resolve(res) num = 0 } catch(e){ if(!num) reject(e) } num -- } })...

方法一:悬浮框圈出满足条件的字符串,时间复杂度O(n),空间复杂度O(n), ``` javascript /** * @param {string} str * @return {number} */ function lengthOfLongestSubstring(str){ if(!str || str.length < 0) return 0; let num = 0; let strArr = str.split(''); let...

递归 ``` js /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** *...

``` javascript /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param...

解题思路: 新建Stack ; 遍历字符串S => S[i]; if Stack.top() === S[i] => Stack.pop else => Stack.push(S[i]) ``` javascript function Duplicates(S){ let Stack = [] let L = 0 let SL =...

> @fanefanes > 提个小建议: 变量`L`可以去掉的, 因为`L === Stack.length` 遍历的时候尽量赋值常量, 要不然for每次循环会进行一次数组遍历查询 获取length

方法: 检测这个字符串,是否符合一个完整出入栈流程, '(','{','[' 这类字符串为入栈 stack.push(), ')','}',']' 这类字符串为出栈 stack.pop(), 当执行结束stack.length = 0 时,return true 时间复杂度O(n), 空间复杂度O(n) 代码就是楼上的for 循环解释

死办法,循环比较 终止 ``` javascript let array = ["flower","flow","flght", ...] function parse(arr){ let repetition = array[0] arr.forEach(a=>{ for(let i=repetition.length-1;i>=0;i--){ repetition = repetition.split("",i+1).join("") if(a.indexOf(repetition) > -1) return if(i === 0) repetition =...

递归,记录最大长度 ``` javascript function maxTreeCount(root){ if(!root) return 0 let max = 1 TreeCount(root, 1) function TreeCount(node, num){ max= Math.max(max, num++) if(node.left)TreeCount(node.left, num) if(node.right)TreeCount(node.right, num) } return max } ``` 迭代实现...