syc
syc
中心扩散法 ```javascript function fn(str) { if(!str.length) return '' let res = str[0] const setRes = (j, k) => { if(str[j] !== str[k]) return while (str[j]&&str[j] === str[k]&&str[k]) { res =...
避免+0,-0,1/-Infinity,1/Infinity都相等。 ```javascript function is(x, y) { if (x === y) { return x !== 0 || y !== 0 || 1 / x === 1 / y; } else {...
一圈一圈删除 ```javascript function fn(arr) { const res = [] while (arr.length) { res.push(...arr.shift()) if (arr.length) { const last = arr.pop() if (arr.length) { arr.forEach(val => { res.push(val.pop()) }) } res.push(...last.reverse())...
str.replace(/ +/g,' ').trim().split(' ').reverse().join(' ')
```javascript function fn(str) { if (!str) return {} //vals存储当前满足的字符,max是数量,prev是上个字符,prevNum是上个字符的数量 let vals = [], max = 1 let prev = '', prevNum = 1 for (const le of str) { if...
感觉递归函数不需参数也可以 ```javascript function fn(arr) { const path = [], used = {} const res = [] const loopFn = () => { if (path.length === arr.length) { res.push([...path]) return }...
```javascript function fn(root,node1,node2) { let res = 0 const getParent = (root, node1, node2) => { if (!root || root === node1 || root === node2) return root const left...
```javascript const fun = (input) => { if(typeof input !== 'object') return input if(input instanceof Array) { return input.map(val=>fun(val)) } if(input instanceof Object) { const obj = {} for(val in...
```javascript const fun = (tree) => { //迭代 let res = [] const stack = [] if(tree) stack.push(tree) while(stack.length>0){ const chTree = stack.pop() if(chTree.left) stack.push(chTree.left) if(chTree.right) stack.push(chTree.right) res.unshift(chTree.val) } return...
进阶部分,先把两个链表倒置,再相加 ```javascript function fn(l1, l2) { //函数导致 const reverseFn = (l) => { let prev = null, curr = l while (curr) { const temp = curr.next curr.next = prev...