测试开辟备战秋招口试10-牛客刷题之堆/栈/队列

打印 上一主题 下一主题

主题 2040|帖子 2040|积分 6120

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
努力了那么多年,回头一望,险些满是漫长的挫折和煎熬。对于大多数人的一生来说,顺风顺水只是偶尔,挫折、不堪、焦急和迷茫才是主旋律。我们登上并非我们所选择的舞台,演出并非我们所选择的剧本。继续加油吧!
目次
1、用两个栈实现队列
2、 包罗min函数的栈
3、有用括号序列
4、滑动窗口的最大值
5、最小的K个数
6、探求第K大
7、数据流中的中位数


1、用两个栈实现队列

题目链接:用两个栈实现队列_牛客题霸_牛客网
思绪:元素全部入栈1,然后出栈入栈2,最后从栈2全部出栈。
Java版:
  1. import java.util.Stack;
  2. public class Solution {
  3.     Stack<Integer> stack1 = new Stack<Integer>();
  4.     Stack<Integer> stack2 = new Stack<Integer>();
  5.    
  6.     public void push(int node) {
  7.        stack1.push(node) ;
  8.     }
  9.    
  10.     public int pop() {
  11.         if(stack2.isEmpty()){
  12.             while(!stack1.isEmpty()){
  13.                 stack2.add(stack1.pop()) ;
  14.             }
  15.         }
  16.         return stack2.pop() ;
  17.       
  18.     }
  19. }
复制代码

2、 包罗min函数的栈

题目链接:包罗min函数的栈_牛客题霸_牛客网
思绪:利用两个栈,一个栈用于维护入栈和出栈,取栈顶元素,另一个栈用于维护单调栈,也是取栈顶元素。
Java版:
  1. import java.util.Stack;
  2. public class Solution {
  3.     Stack<Integer> s1 = new Stack<>() ;
  4.     Stack<Integer> s2 = new Stack<>() ;
  5.     public void push(int node) {
  6.         s1.push(node) ;
  7.         if(s2.isEmpty() || s2.peek() > node){
  8.             s2.push(node) ;
  9.         }else{
  10.             s2.push(s2.peek()) ;
  11.         }
  12.     }
  13.    
  14.     public void pop() {
  15.         s1.pop() ;
  16.         s2.pop() ;
  17.     }
  18.    
  19.     public int top() {
  20.        return  s1.peek() ;
  21.     }
  22.    
  23.     public int min() {
  24.         return s2.peek() ;
  25.     }
  26. }
复制代码
3、有用括号序列

题目链接:有用括号序列_牛客题霸_牛客网
思绪:每次如果碰到左括号,就让右括号入栈,每次碰到右括号,出栈匹配。
Java版:
 
  1. import java.util.*;
  2. public class Solution {
  3.     /**
  4.      *
  5.      * @param s string字符串
  6.      * @return bool布尔型
  7.      */
  8.     public boolean isValid (String s) {
  9.         // write code here
  10.         Stack<Character> s1 = new Stack<>() ;
  11.         for(int i=0; i<s.length(); i++){
  12.             if(s.charAt(i) == '{'){
  13.                 s1.add('}') ;
  14.             }else if(s.charAt(i) == '('){
  15.                 s1.add(')') ;
  16.             }else if(s.charAt(i) == '['){
  17.                 s1.add(']') ;
  18.             }else if(s1.isEmpty() || s1.pop() != s.charAt(i)){
  19.                 return false ;
  20.             }
  21.         }
  22.         return s1.isEmpty() ;
  23.     }
  24. }
复制代码
4、滑动窗口的最大值

题目链接:滑动窗口的最大值_牛客题霸_牛客网
思绪:袒露罗列全部滑动窗口,依次比对全部窗口中值的巨细即可。
暴力版:
  1. import java.util.*;
  2. public class Solution {
  3.     public ArrayList<Integer> maxInWindows(int [] num, int size) {
  4.         ArrayList<Integer> arrayList = new ArrayList<>() ;
  5.         if(num.length == 0 || size > num.length || size == 0){
  6.             return arrayList ;
  7.         }
  8.         for(int i=0; i<num.length-size+1; i++){
  9.             int max = num[i] ;
  10.             for(int j=i+1; j<i+size; j++){
  11.                 max = Math.max(max, num[j]) ;
  12.             }
  13.             arrayList.add(max) ;
  14.         }
  15.         return arrayList ;
  16.     }
  17. }
复制代码
思绪2:维护一个双端的单调队列,让每次背面入队的都比前面大,每一个新窗口,必要队列头部元素出队。
  1. import java.util.*;
  2. public class Solution {
  3.     public ArrayList<Integer> maxInWindows(int [] num, int size) {
  4.         //利用双端队列,每次尾部进入保证比之前的小,每次下一个窗口时,移除头部元素
  5.         ArrayList<Integer> arrayList = new ArrayList<Integer>() ;
  6.         ArrayDeque<Integer> arrayDeque = new ArrayDeque<Integer>() ;
  7.         if(size==0 || num.length == 0 || size > num.length){
  8.             return arrayList ;
  9.         }
  10.         for(int i=0; i<size; i++){
  11.             while(!arrayDeque.isEmpty() && num[arrayDeque.getLast()] < num[i]){
  12.                 arrayDeque.pollLast() ;
  13.             }
  14.             arrayDeque.addLast(i) ;
  15.         }
  16.         for(int i=size; i<num.length; i++){
  17.             arrayList.add(num[arrayDeque.peekFirst()]) ;
  18.             while(!arrayDeque.isEmpty() && num[arrayDeque.getLast()] < num[i]){
  19.                 arrayDeque.pollLast() ;
  20.             }
  21.             while(!arrayDeque.isEmpty() &&  (i-size+1) > arrayDeque.peekFirst()){
  22.                 arrayDeque.pollFirst() ;
  23.             }
  24.             arrayDeque.addLast(i) ;
  25.         }
  26.         arrayList.add(num[arrayDeque.peekFirst()]) ;
  27.         return arrayList ;
  28.     }
  29. }
复制代码
5、最小的K个数

题目链接:最小的K个数_牛客题霸_牛客网
思绪:直接排序法。
Java版:
  1. import java.util.*;
  2. public class Solution {
  3.     public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
  4.         //直接排序法
  5.         ArrayList<Integer> arr = new ArrayList<>() ;
  6.         Arrays.sort(input) ;
  7.         for(int i=0; i<k; i++){
  8.             arr.add(input[i]) ;
  9.         }
  10.         return arr ;
  11.     }
  12. }
复制代码
思绪2:建立容量为k的大根堆,每次对比堆顶元素,如果当前元素小于堆顶元素,则更换。
  1. import java.util.*;
  2. public class Solution {
  3.     public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
  4.         //堆排序法,建立容量为k的优先队列构成的大根堆
  5.       
  6.         ArrayList<Integer> list = new ArrayList<>() ;
  7.          if(input.length == 0 || k == 0){
  8.             return list ;
  9.          }
  10.         PriorityQueue<Integer> queue = new PriorityQueue<>((o1,o2)->o2-o1) ;
  11.         for(int i=0; i<input.length; i++){
  12.             if(queue.size() < k){
  13.                 queue.add(input[i]) ;
  14.             }else{
  15.                 if(input[i] < queue.peek()){
  16.                     queue.poll() ;
  17.                     queue.add(input[i]) ;
  18.                 }
  19.             }
  20.         }
  21.         while(!queue.isEmpty()){
  22.             list.add(queue.peek()) ;
  23.             queue.poll() ;
  24.         }
  25.         return list ;
  26.     }
  27. }
复制代码
6、探求第K大

题目链接:探求第K大_牛客题霸_牛客网
思绪:直接排序,然后返回倒数第K个就行了。
Java版:
  1. import java.util.*;
  2. public class Solution {
  3.     public int findKth(int[] a, int n, int K) {
  4.         // write code here
  5.         Arrays.sort(a) ;
  6.         return a[n - K] ;
  7.     }
  8. }
复制代码
7、数据流中的中位数

题目链接:数据流中的中位数_牛客题霸_牛客网
思绪:先排序,然后取中位数即可。
Java版:
  1. import java.util.* ;
  2. public class Solution {
  3.     ArrayList<Integer> arrayList = new ArrayList<>() ;
  4.     public void Insert(Integer num) {
  5.         arrayList.add(num) ;
  6.         Collections.sort(arrayList) ;
  7.     }
  8.     public Double GetMedian() {
  9.         int n = arrayList.size() ;
  10.         double result = (n%2==1)  ? (double)arrayList.get(n/2) : (double)(arrayList.get(n/2) + arrayList.get(n/2-1)) / 2 ;  
  11.         return result ;
  12.     }
  13. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

我可以不吃啊

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表