AlgorithmsI PA2: Randomized Queues and Deques  Subset

AlgorithmsI PA2: Randomized Queues and Deques  Subset

本题的bonus是

AlgorithmsI PA2: Randomized Queues and Deques  Subset

因此方法是queue的size 达到了K, 就停止增加元素,保证queue.size() 最大时只有k.


 Java code:

import edu.princeton.cs.algs4.StdIn; 
import edu.princeton.cs.algs4.StdOut; 

public class Subset {
    public static void main(String[] args){
          int k = Integer.parseInt(args[0]);
          
          RandomizedQueue<String> queue = new RandomizedQueue<String>();
      
          while(StdIn.hasNextLine() && !StdIn.isEmpty()) {
              if(queue.size() < k) {
                  queue.enqueue(StdIn.readString());
              }else {
                 break;
              }                            
          }
         
          for(int i = 0; i< k; i++) {
              StdOut.println(queue.dequeue());
          }   
    }
}

 

相关文章:

  • 2021-10-07
  • 2021-10-20
  • 2021-06-09
  • 2022-12-23
  • 2022-12-23
  • 2022-02-28
  • 2021-06-12
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-10
  • 2021-12-26
  • 2021-09-08
  • 2022-03-02
相关资源
相似解决方案