JavaScript算法题总结 (四)堆/栈/队列

BM42 用两个栈实现队列

在这里插入图片描述

let stack1=[],stack2=[];
function push(node)
{
    // write code here
    stack1.push(node);
}
function pop()
{
    // write code here
    if(stack2.length==0){
        while(stack1.length!==0){
            stack2.push(stack1.pop());
        }
    }
    return stack2.pop()
}
module.exports = {
    push : push,
    pop : pop
};

BM43 包含min函数的栈

在这里插入图片描述

BM43 包含min函数的栈

在这里插入图片描述

let stack1=[];

function push(node)
{
    // write code here
    stack1.push(node);
}
function pop()
{
    // write code here
    stack1.pop();
}
function top()
{
    // write code here
    return stack1[stack1.length-1];
}
function min()
{
    // write code here
    let min=stack1[0];
    for(let i=1;i<stack1.length;i++){
        min=min>stack1[i]?stack1[i]:min
    }
    return min
}
module.exports = {
    push : push,
    pop : pop,
    top : top,
    min : min
};

BM44 有效括号序列

在这里插入图片描述

/**
  * 
  * @param s string字符串 
  * @return bool布尔型
  */
function isValid( s ) {
    // write code here
    let stack=[];
    for(let i=0;i<s.length;i++){
        if(s[i]==='('||s[i]==='{'||s[i]==='['){
            stack.push(s[i]);
        }else{
            if(s[i]===')'&&stack.pop()==='('||
              s[i]===']'&&stack.pop()==='['||
              s[i]==='}'&&stack.pop()==='{'){
                continue;
            }else{
                return false;
            }
        }
    }
    if(stack.length!==0){
        return false
    }
    return true
    
}
module.exports = {
    isValid : isValid
};

BM46 最小的K个数

在这里插入图片描述

function GetLeastNumbers_Solution(input, k)
{
  function swap(i,j){
    let temp = heap[i];
    heap[i] = heap[j];
    heap[j] = temp;
  }
  function percDown(index,N){
    let root = heap[index];
    
    let parent,child;
    //为root找到合适的位置
    for( parent=index; parent*2+1<N; parent=child){
      child = parent*2+1;
      //child为两个parent中值小的那个元素
      if(child+1<N && heap[child+1]>heap[child])
        child++;
      
      if( root > heap[child] )//找到了合适位置break
        break;
      else
        heap[parent] = heap[child];
    }
    
    heap[parent] = root;
  }
  
  let heap = input.slice(0,k);
  
  //前k个元素建立最大堆
  for(let i=Math.floor(k/2)-1; i>=0; i-- ){
    percDown(i,k);//第i个到第N个为最大堆
  }
  
  for(let i=k; i<input.length; i++){
    if(input[i]<heap[0]){
      heap[0] = input[i];//新的元素比大顶堆的第一个还小的话,替换大顶堆的第一个元素,重新建堆
      percDown(0,k);
    }
  }
  
  return heap;
}
module.exports = {
    GetLeastNumbers_Solution : GetLeastNumbers_Solution
};

BM47 寻找第K大

在这里插入图片描述

/**
 * 
 * @param a int整型一维数组 
 * @param n int整型 
 * @param K int整型 
 * @return int整型
 */
function findKth( a ,  n ,  K ) {
    // write code here
    if(a.length===0) return 0;
    return sortArray(a)[n-K]
}
function sortArray(nums){
    let len=nums.length;
    if(len===0) return nums;
    const midIndex=Math.floor(len/2);
    const midValue=nums.splice(midIndex,1)[0];
    const left=[];
    const right=[];
    for(let i=0;i<nums.length;i++){
        const n=nums[i];
        if(n<midValue) left.push(n);
        else right.push(n);
    }
    return sortArray(left).concat([midValue],sortArray(right))
}
module.exports = {
    findKth : findKth
};

BM48 数据流中的中位数

在这里插入图片描述

let arr=[];
function Insert(num)
{
    // write code here
    let i=0;
    while(arr[i]<num) i++;
    arr.splice(i,0,num);
}
function GetMedian(){
	// write code here
     let index = Math.floor(  arr.length/2 )
  if(arr.length%2){//奇数
    return arr[index];
  }else{
    return ( arr[index] + arr[index-1] )/2 ;
  }
}
module.exports = {
    Insert : Insert,
    GetMedian : GetMedian
};

BM49 表达式求值

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值