let stack1 =[],stack2 =[]
function push(node){// write code here
stack1.push(node)}
function pop(){// write code hereif(!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 hereif(!stack.length)return
stack.pop()
minstack.pop()}
function top(){// write code hereif(!stack.length)returnreturn stack[stack.length -1]}
function min(){// write code hereif(!stack.length)returnreturn 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
elseif(temp ==']'&& pop !=='[')return false
elseif(temp =='}'&& pop !=='{')return false
}}return stack.length ==0}
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=0while(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 herereturneval(s)}