📜  反转给定 Stack 的前 K 个元素

📅  最后修改于: 2022-05-13 01:57:09.072000             🧑  作者: Mango

反转给定 Stack 的前 K 个元素

给定一个栈S和一个整数K ,任务是反转给定栈的前 K 个元素
例子

方法:可以使用两个辅助堆栈反转给定堆栈的前K个元素来解决该任务。请按照以下步骤解决问题:

  • 创建两个堆栈 s1 和 s2
  • 一个接一个地弹出给定堆栈的所有元素并同时将其压入堆栈 s1
  • 现在,从 s1 中弹出 k 个元素并同时将其推入 s2
  • 从堆栈 s2 中弹出所有元素,并将它们推入给定的堆栈
  • 类似地从 s1 中弹出所有元素,并将它们推送到给定的堆栈中

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to print the resultant stack
void print(stack& s)
{
    vector v;
    while (!s.empty()) {
        int temp = s.top();
        s.pop();
        v.push_back(temp);
    }
 
    reverse(v.begin(), v.end());
    for (int i = 0; i < v.size(); i++)
        cout << " " << v[i] << " ";
}
 
// Function to reverse and push operation
// in the stack
void stack_reverse(stack& s, int k)
{
    stack s1, s2;
    while (!s.empty()) {
        int temp = s.top();
        s1.push(temp);
        s.pop();
    }
 
    for (int i = 0; i < k; i++) {
        int temp = s1.top();
        s2.push(temp);
        s1.pop();
    }
 
    while (!s2.empty()) {
        int temp = s2.top();
        s.push(temp);
        s2.pop();
    }
 
    while (!s1.empty()) {
        int temp = s1.top();
        s.push(temp);
        s1.pop();
    }
}
 
// Driver Code
int main()
{
    stack s;
    // s = [ 1, 2, 3, 4, 5, 8, 3, 0, 9 ]
    s.push(1);
    s.push(2);
    s.push(3);
    s.push(4);
    s.push(5);
    s.push(8);
    s.push(3);
    s.push(0);
    s.push(9);
 
    int k = 4;
    stack_reverse(s, k);
    print(s);
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
  static Stack s = new Stack<>();
   
  // Function to print the resultant stack
  static void print()
  {
    Vector v = new Vector<>();
    while (!s.isEmpty()) {
      int temp = s.peek();
      s.pop();
      v.add(temp);
    }
 
    Collections.reverse(v);
    for (int i = 0; i < v.size(); i++)
      System.out.print(" " +  v.get(i)+ " ");
  }
 
  // Function to reverse and push operation
  // in the stack
  static void stack_reverse(int k)
  {
    Stack s1 = new Stack<>(), s2 =new Stack<>();
    while (!s.isEmpty()) {
      int temp = s.peek();
      s1.add(temp);
      s.pop();
    }
 
    for (int i = 0; i < k; i++) {
      int temp = s1.peek();
      s2.add(temp);
      s1.pop();
    }
 
    while (!s2.isEmpty()) {
      int temp = s2.peek();
      s.add(temp);
      s2.pop();
    }
 
    while (!s1.isEmpty()) {
      int temp = s1.peek();
      s.add(temp);
      s1.pop();
    }
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    // s = [ 1, 2, 3, 4, 5, 8, 3, 0, 9 ]
    s.add(1);
    s.add(2);
    s.add(3);
    s.add(4);
    s.add(5);
    s.add(8);
    s.add(3);
    s.add(0);
    s.add(9);
 
    int k = 4;
    stack_reverse(k);
    print();
  }
}
 
// This code is contributed by gauravrajput1


Python3
# python3 program for the above approach
 
 
# Function to print the resultant stack
def print(s):
 
    v = []
    while (len(s) != 0):
        temp = s.pop()
        v.append(temp)
 
    v.reverse()
    for i in range(0, len(v)):
        print(f" {v[i]} ", end="")
 
 
# Function to reverse and push operation
# in the stack
def stack_reverse(s, k):
 
    s1, s2 = [], []
    while (len(s) != 0):
        temp = s.pop()
        s1.append(temp)
 
    for i in range(0, k):
        temp = s1.pop()
        s2.append(temp)
 
    while (len(s2) != 0):
        temp = s2.pop()
        s.append(temp)
 
    while (len(s1) != 0):
        temp = s1.pop()
        s.append(temp)
 
 
# Driver Code
if __name__ == "__main__":
 
    s = []
    # s = [ 1, 2, 3, 4, 5, 8, 3, 0, 9 ]
    s.append(1)
    s.append(2)
    s.append(3)
    s.append(4)
    s.append(5)
    s.append(8)
    s.append(3)
    s.append(0)
    s.append(9)
 
    k = 4
    stack_reverse(s, k)
    print(s)
 
    # This code is contributed by rakeshsahni


Javascript


输出
4  3  2  1  5  8  3  0  9 


时间复杂度:O(N),N是栈中元素的个数
辅助空间:O(N)