Last active
November 13, 2015 17:44
-
-
Save gregorykelleher/2a92ec8541ca70bfda6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import java.util.Stack; | |
public class Solution { | |
public static void main(String[] args) { | |
Scanner scan = new Scanner(System.in); | |
//System.out.println("Enter the size of the stack: "); | |
int size = scan.nextInt(); | |
scan.nextLine(); | |
Stack stack = new Stack(); | |
int countSize = size; | |
int maxSoFar = 0; | |
while (countSize != 0) { | |
//System.out.println("Enter the command: "); | |
String str = scan.nextLine(); | |
int length = str.length(); | |
String NumCmd = ""; | |
for (int i = 0; i < length; i++) { | |
Character character = str.charAt(i); | |
if (Character.isDigit(character)) { | |
NumCmd += character; | |
} | |
} | |
if(NumCmd == "") { | |
if (str.equals("PEEK")) { | |
stack.peek(); | |
System.out.println(stack.peek()); | |
} | |
if (str.contains("POP")) { | |
stack.pop(); | |
System.out.printf("%s ", stack); | |
} | |
} | |
else { | |
int x = Integer.parseInt(NumCmd.valueOf(NumCmd)); | |
if (maxSoFar < x) { | |
maxSoFar = x; | |
} | |
if (str.contains("PUSH")) { | |
stack.push(x); | |
countSize--; | |
} | |
} | |
} | |
System.out.println(maxSoFar); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"countSize--" should not be included for POP and PEEK