2021春招冲刺 12-23

202 阅读1分钟

操作系统

进程间如何通信

  1. 管道通信
  2. 消息队列
  3. 共享内存
  4. 信号量
  5. socket通信

线程间如何通信

  1. 共享内存(互斥)
  2. 事件(信号),Wait/Notify
  3. 信号量
  4. while轮询
  5. 使用基本LockSupport实现线程间的阻塞和唤醒

算法与数据结构

有效括号

给定一个只包括 []{}() 的字符串,判断字符串是否有效。

输入: "()"
输出: true
输入: "[()]"
输出: true
输入: "{()]"
输出: false

代码如下

//正则表达式
function kuohaopipei(data){
   while(/\(\)|\[\]|\}\{/g.test(data)){
       data = data.replace(/\(\)|\[\]|\{\}/g,"")
   }
   return !data.length
}
console.log(kuohaopipei("()"),kuohaopipei("[()]"),kuohaopipei("{()]")) 

//传统的思想就是利用栈
        const kuohaopipei = (str) => {
            const [first,...others] = str;   
            const stack = [first];   
            while(others.length > 0){
                const l = stack[stack.length - 1];    
                const r = others.shift(); 
                if(!mathch(l,r)){
                    stack.push(r);
                }else{
                    stack.pop();
                }
            }
            return stack.length === 0;
        }
const mathch = (l,r) => {
    return  (l == '(' && r == ')') || (l == '{' && r == '}') || (l == '[' && r == ']');
}
console.log(kuohaopipei("()"),kuohaopipei("[()]"),kuohaopipei("{()]"))

数组转换

把一个一维数组(数据只包含Number)[2,3,4]转为234你有哪些方式

let arr = [1,2,3]
console.log(arr.toString().replace(/,/g,''))
arr = arr.reduce((prev,now) =>{
    return prev.toString() + now.toString()
})
console.log(arr)
[2,3,4].join("")

数组展平

一个多维数组,转换成一维数组,方案有多种,能写几种算几种

function _flat(){ // ...code } _flat([1,[2,[3]]]) // [1,2,3]

//使用可迭代对象
 function* arrFlat(item){
    if(Array.isArray(item)){
        for(let i = 0 ;i<item.length;i++){
            yield* arrFlat(item[i]);
        }
    }else{
        yield item;
    }
}
for (const x of arrFlat(arr1)) {
    console.log(x);
}
console.log(arrFlat(arr1))
//递归判断有没有数组 有就展开

function arrFlat2(arr) {
    while (arr.some(item => Array.isArray(item))) {
        arr = [].concat(...arr);
    }
    return arr;
}

// 使用reduce方法
function arrFlat3(arr) {
    var newArr = arr.reduce((prev, current) => {
         return prev.concat(Array.isArray(current) ? arrFlat3(current) : current)
    }, []);
    return newArr;
}
//使用es6的flat方法
arr.flat(arr)