// 定义一个多层嵌套的对象 {a:{b:{c:1}}},写一个函数,如果传入 ‘a,b,c’ 则返回true,否则返回false
function deepMatch(str, obj){
if(typeof str !== 'string' || typeof obj !== 'object') return false
const strArray = str.split(',')
const deepFind = (index, obj) => {
if(index === strArray.length){
return true
}
const value = obj[strArray[index]]
if(value){
return deepFind(++index, value)
}else{
return false
}
}
return deepFind(0, obj)
}
console.log(deepMatch('a,b,c', {a:{b:{c:1}}}))
// # 二叉树前序、中序、后序遍历的JavaScript实现
// ```
// 前序
const Tree = {
val: 1,
left: {
val: 2,
left: { val: 4 },
right: { val: 5 },
},
right: {
val: 3,
left: { val: 6 },
right: { val: 7 },
},
};
```
//前序
function getNodeArr(Tree){
const list = [];
function sum(node){
if(node.val){
list.push(node.val)
node?.left && sum(node.left)
node?.right && sum(node.right)
}else{
return
}
}
sum(Tree)
return list
}
console.log(getNodeArr(Tree));
//后序
function getNodeArr(Tree){
const list = [];
function sum(node){
if(node.val){
node?.left && sum(node.left)
node?.right && sum(node.right)
list.push(node.val)
}else{
return
}
}
sum(Tree)
return list
}
console.log(getNodeArr(Tree));
// # JS写斐波那契数列
// 深度优先
function sum(n){
let n1 = 1; n2= 1;sum2 = 0;
for(let i=2; i<30; i++){
sum2 = n1 + n2;
n1 = n2;
n2 = sum2;
}
return sum2;
}
console.log(sum(30))
function sumvalue(n){
const sumArr = [0, 1];
function func(n){
if(sumArr[n] == undefined){
sumArr[n] = func(n-1) + func(n-2)
return sumArr[n]
}else{
return sumArr[n]
}
}
return func(n)
}
console.log(sumvalue(30))
//柯里化
unction add(...arg){
let sum = arg || []
function count(...n){
if(n.length){
sum= [...sum, ...n]
return count
}
}
count.valueof = function (){return sum.reduce((a,b)=>{return a+b})}
return count;
}
+add(1)(2)(3)(4)(5)
async function async1(){
console.log('async1 start')
await async2()
console.log('async1 end')
}
async function async2(){
console.log('async2')
}
console.log('script start')
setTimeout(function(){
console.log('setTimeOut1)
new Promise(function(resolve){
console.log('promise3’)
resolve()
}).then(function(){
console.log('promise4’)
})
}, 0)
setTimeout(function(){
console.log('setTimeOut2’)
}, 0)
async1()
new Promise(function(resolve){
console.log('promise1')
resolve()
}).then(function(){
console.log('promise2')
})
console.log('script end')
运行结果:
script start
async1 start
async2
promise1
script end
async1 end
promise2
setTimeOut1
promise3
promise4
setTimeOut2
获取 url 中的参数\
1. 指定参数名称,返回该参数的值 或者 空字符串\
2. 不指定参数名称,返回全部的参数对象 或者 {}\
3. 如果存在多个同名参数,则返回数组
4. 不支持URLSearchParams方法
输入:
http://www.nowcoder.com?key=1&key=2&key=3&test=4#hehe key
输出:
[1, 2, 3]
const str = 'http://www.nowcoder.com?key=1&key=2&key=3&test=4#hehe';
const name = 'key';
const paramsArr = str?.split('#')?.[0]?.split('?')?.[1]?.split('&') || [];
const urlObj = {}
paramsArr.map(item=>{
const itemv = item.split('=');
if(urlObj[itemv[0]]){
urlObj[itemv[0]].push(itemv[1]);
}else{
urlObj[itemv[0]] = [itemv[1]]
}
})
console.log(urlObj[name]);
给定一个长度为 n 的可能有重复值的数组,找出其中不去重的最小的 k 个数。例如数组元素是4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4(任意顺序皆可)。
反转链表
反转链表
/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function ReverseList(list)
{
// write code here
if(JSON.stringify(list) === '{}'){
return {}
}
let pre = null;
let cur = list;
while(cur !== null){
let next = cur.next;
cur.next = pre;
pre = cur;
cur = next
}
return pre
}
module.exports = {
ReverseList : ReverseList
};
设计LRU缓存结构
描述
设计LRU(最近最少使用)缓存结构,该结构在构造时确定大小,假设大小为 k ,并有如下两个功能
-
set(key, value):将记录(key, value)插入该结构
-
get(key):返回key对应的value值 提示:
1.某个key的set或get操作一旦发生,认为这个key的记录成了最常使用的,然后都会刷新缓存。
2.当缓存的大小超过k时,移除最不经常使用的记录。
3.输入一个二维数组与k,二维数组每一维有2个或者3个数字,第1个数字为opt,第2,3个数字为key,value
若opt=1,接下来两个整数key, value,表示set(key, value)
若opt=2,接下来一个整数key,表示get(key),若key未出现过或已被移除,则返回-1
对于每个opt=2,输出一个答案\
4.为了方便区分缓存里key与value,下面说明的缓存里key用""号包裹
\
要求:set和get操作复杂度均为 O(1)O(1)
/**
* lru design
* @param operators int整型二维数组 the ops
* @param k int整型 the k
* @return int整型一维数组
*/
function LRU( operators , k ) {
if(!operators.length){
return []
}
const list = new Map();
let result = [];
let keys = []
operators.map(item => {
if(item[0] === 1){
set(item[1], item[2])
}
if(item[0] === 2){
result = get(item[1])
}
});
function set(key, value){
if(list.has(key)){
keys.splice(keys.indexOf(key), 1)
list.set(key, [value, ...list.get(key)])
}else{
list.set(key, [value])
}
if(list.size > k){
const deletekey = keys.shift();
list.delete(deletekey)
}
keys.push(key)
}
function get(key){
updateKey(key)
return list.get(key) || [1, -1];
}
function updateKey(key){
keys.splice(keys.indexOf(key), 1)
keys.push(key)
}
return result;
// write code here
}
module.exports = {
LRU : LRU
};