【7.12】算法复习四 堆/栈/队列

7.12

  • 42 用两个栈实现队列
let stack1 = [],stack2 = []
function push(node)
{
    // write code here
    stack1.push(node)
}
function pop()
{
    // write code here
    if(!stack2.length){
        while(stack1.length){
            stack2.push(stack1.pop())
        }
    }
    return stack2.pop()
}
  • 43 包含min函数的栈
let stack = [],minstack = []
function push(node)
{
    // write code here
    stack.push(node)
    if(!minstack.length) minstack.push(node)
    else{
        if(node<minstack[minstack.length-1]) minstack.push(node)
        else{
            minstack.push(minstack[minstack.length-1])
        }
    }
}
function pop()
{
    // write code here
    if(!stack.length) return
    stack.pop()
    minstack.pop()
}
function top()
{
    // write code here
    if(!stack.length) return
    return stack[stack.length - 1]
}
function min()
{
    // write code here
    if(!stack.length) return
    return minstack[minstack.length - 1]
}
  • 44 有效括号序列
function isValid( s ) {
    // write code here
    let stack = []
    for(let i=0;i<s.length;i++){
        let temp = s.charAt(i)
        if(temp == '(' || temp == '[' || temp == '{'){
            stack.push(temp)
        }else{
            if(!stack.length) return false
            let pop = stack.pop()
            if(temp == ')' && pop !== '(') return false
            else if(temp == ']' && pop !== '[') return false
            else if(temp == '}' && pop !== '{') return false
        }
    }
    return stack.length == 0
}
  • 45 滑动窗口的最大值 【好难】
function maxInWindows(num, size)
{
    // write code here
    if(size>num.length || !size) return []
    let queue = [],arr = []
    for(let i=0;i<num.length;i++){
        if(!queue.length) queue.push(i)
        else if(num[queue[queue.length-1]]<=num[i]){
            while(num[queue[queue.length-1]]<=num[i]){
                queue.pop()
            }
            queue.push(i)
        }else{
            queue.push(i)
        }
        if(queue.length && i-queue[0]>=size){
            queue.shift()
        }
        if(i >= size-1){
            arr.push(num[queue[0]])
        }
    }
    return arr
}
  • 46 最小的k个数
function GetLeastNumbers_Solution(input, k)
{
    // write code here
    function quicksort(arr){
        if(arr.length<2) return arr
        let left = [],right = []
        let pivot = arr[0]
        for(let i = 1;i<arr.length;i++){
            arr[i]<pivot ? left.push(arr[i]):right.push(arr[i])
        }
        return [...quicksort(left),pivot,...quicksort(right)]
    }
    let res = quicksort(input)
    return res.slice(0,k)
}
  • 47 寻找第k大 【v8引擎的sort()方法采用的是插入(数组长度≤10)和快排】
function findKth( a ,  n ,  K ) {
    // write code here
    a.sort((x,y)=>x-y)
    return a[n-K]
}
  • 48 数据流的中位数
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 mid = Math.floor(arr.length/2)
    if(arr.length % 2){
        return arr[mid]
    }else{
        return (arr[mid]+arr[mid-1])/2
    }
}
  • 49 表达式求值【js直接eval()即可】
function solve( s ) {
    // write code here
    return eval(s)
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值