Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // you can also use imports, for example:
- import java.util.*;
- // you can use System.out.println for debugging purposes, e.g.
- // System.out.println("this is a debug message");
- public class Solution {
- private List<Integer> stack=new ArrayList<>();
- private ArrayList<Integer> indices = new ArrayList<>();
- private boolean transactionStart=false;
- public Solution() {
- // write your code in Java SE 8
- }
- public void push(int value) {
- this.stack.add(value);
- }
- public int top() {
- if(this.stack.size() > 0 ){
- return this.stack.get(this.stack.size()-1);
- }
- return 0;
- }
- public void pop() {
- if(this.stack.size() > 0 ) {
- this.stack.remove(this.stack.size()-1);
- }
- }
- public void begin() {
- this.indices.add(this.stack.size()-1);
- this.transactionStart = true;
- }
- public boolean rollback() {
- if(this.indices.size() > 0 ){
- this.stack = this.stack.subList(0,this.indices.get(this.indices.size()-1)+1);
- this.indices.remove(this.indices.size()-1);
- return true;
- }
- return false;
- }
- public boolean commit() {
- if(this.indices.size() > 0 ) {
- this.indices.remove(this.indices.size()-1);
- return true;
- }
- return false;
- }
- public static void test() {
- // Define your tests here
- Solution sol = new Solution();
- sol.push(42);
- sol.push(2);
- assert sol.top() == 2 : "top() should be 42";
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement