本文整理汇总了Java中edu.princeton.cs.algs4.Stack.push方法的典型用法代码示例。如果您正苦于以下问题:Java Stack.push方法的具体用法?Java Stack.push怎么用?Java Stack.push使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类edu.princeton.cs.algs4.Stack
的用法示例。
在下文中一共展示了Stack.push方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: BruteCollinearPoints
import edu.princeton.cs.algs4.Stack; //导入方法依赖的package包/类
public BruteCollinearPoints(Point[] points) {
if (points == null || hasNull(points) || hasDuplicate(points)) throw new IllegalArgumentException();
Stack<LineSegment> segmentstack = new Stack<>();
int N = points.length;
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
for (int k = j+1; k < N; k++) {
for (int l = k+1; l < N; l++) {
Point[] ps = {points[i], points[j], points[k], points[l]};
Arrays.sort(ps);
if (collinear(ps)) segmentstack.push(new LineSegment(ps[0], ps[3]));
}
}
}
}
int size = segmentstack.size();
segments = new LineSegment[size];
for (int i = 0; i < size; i++) this.segments[i] = segmentstack.pop();
}
示例2: searchValidWords
import edu.princeton.cs.algs4.Stack; //导入方法依赖的package包/类
private void searchValidWords(int v, Node x, String prefix, Stack<Integer> visitingDices) {
if (prefix.length() > 2 && x != null && x.val == 1) {
validWords.add(prefix);
}
for (int w : adj[v]) {
char c = getLetterOnBoard(w);
if (!marked[w] && x != null && x.next[c -'A'] != null) {
visitingDices.push(w);
marked[w] = true;
if (c == 'Q') {
searchValidWords(w, x.next['Q'-'A'].next['U' - 'A'], prefix + "QU", visitingDices);
}
else {
searchValidWords(w, x.next[c -'A'], prefix + c, visitingDices);
}
int index = visitingDices.pop();
marked[index] = false;
}
}
}
示例3: solveExpressionOnTopOfStack
import edu.princeton.cs.algs4.Stack; //导入方法依赖的package包/类
private static void solveExpressionOnTopOfStack(Stack<Double> values, String lastOperand) {
//Pop, evaluate and push result
double value = values.pop();
if (lastOperand.equals("+")) {
value = values.pop() + value;
} else if (lastOperand.equals("-")) {
value = values.pop() - value;
} else if (lastOperand.equals("*")) {
value = values.pop() * value;
} else if (lastOperand.equals("/")) {
value = values.pop() / value;
} else if (lastOperand.equals("sqrt")) {
value = Math.sqrt(value);
}
values.push(value);
}
示例4: main
import edu.princeton.cs.algs4.Stack; //导入方法依赖的package包/类
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
stack.push("First Item");
stack.push("Second Item");
stack.push("Third Item");
Stack<String> copy = copy(stack);
stack.pop();
stack.pop();
for (String s : copy) {
StdOut.println(s);
}
StdOut.println("\nExpected: " +
"\nThird Item\n" +
"Second Item\n" +
"First Item");
}
示例5: evaluatePostfix
import edu.princeton.cs.algs4.Stack; //导入方法依赖的package包/类
private static int evaluatePostfix(String postfixExpression) {
Stack<String> operands = new Stack<>();
Stack<String> operators = new Stack<>();
String[] values = postfixExpression.split("\\s");
for (String s : values) {
if (s.equals("(")) {
//do nothing
} else if (s.equals("+")
|| s.equals("-")
|| s.equals("*")
|| s.equals("/")) {
operators.push(s);
} else if (s.equals(")")) {
int operand2 = Integer.parseInt(operands.pop());
int operand1 = Integer.parseInt(operands.pop());
String operator = operators.pop();
int result = 0;
if (operator.equals("+")) {
result = operand1 + operand2;
} else if (operator.equals("-")) {
result = operand1 - operand2;
} else if (operator.equals("*")) {
result = operand1 * operand2;
} else if (operator.equals("/")) {
result = operand1 / operand2;
}
operands.push(String.valueOf(result));
} else {
operands.push(s);
}
}
return Integer.parseInt(operands.pop());
}
示例6: FastCollinearPoints
import edu.princeton.cs.algs4.Stack; //导入方法依赖的package包/类
public FastCollinearPoints(Point[] points) {
if (points == null || hasNull(points) || hasDuplicate(points)) throw new IllegalArgumentException();
Stack<Point[]> segmentstack = new Stack<>();
int N = points.length;
Arrays.sort(points);
Point[] basePoints = points.clone();
for (Point base : basePoints) {
Comparator<Point> slopeorder = base.slopeOrder();
Arrays.sort(points, slopeorder);
for (int j = 1; j < N; j++) {
int c = 1;
while (j + c < N && collinear(new Point[]{base, points[j], points[j + c]})) { c++; }
if (c > 2) {
segmentstack.push(new Point[]{base, points[j + c - 1]});
}
}
}
int size = segmentstack.size();
Point[][] segments = new Point[size][2];
for (int i = 0; i < size; i++) segments[i] = segmentstack.pop();
this.segments = filterSegments(segments);
}
示例7: filterSegments
import edu.princeton.cs.algs4.Stack; //导入方法依赖的package包/类
private static LineSegment[] filterSegments(Point[][] segments) {
Stack<LineSegment> filtered = new Stack<>();
int N = segments.length;
for (int i = 0, c = 0; i < N; i++, c = 0) {
Point[] cur = segments[i], comp, longest = cur;
if (cur == null) { continue; }
while (c < N) {
comp = segments[c];
if (comp != null && isSame(cur, comp)) {
Point[] ps = {longest[0], longest[1], comp[0], comp[1]};
Arrays.sort(ps);
if(longest[0] != ps[0] || longest[1] != ps[3]) longest = new Point[] {ps[0], ps[3]};
segments[c] = null;
}
c++;
}
segments[i] = null;
filtered.push(new LineSegment(longest[0], longest[1]));
}
int size = filtered.size();
LineSegment[] maximals = new LineSegment[size];
for (int i = 0; i < size; i++) maximals[i] = filtered.pop();
return maximals;
}
示例8: main
import edu.princeton.cs.algs4.Stack; //导入方法依赖的package包/类
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
while (!StdIn.isEmpty()) {
String s = StdIn.readString();
if (s.equals("-")) StdOut.print(stack.pop());
else stack.push(s);
}
}
示例9: getPath
import edu.princeton.cs.algs4.Stack; //导入方法依赖的package包/类
private int[] getPath(Pos T) {
Stack<Pos> s = new Stack<>();
for (Pos at = edgeTo[T.y][T.x]; at != null; at = edgeTo[at.y][at.x])
s.push(at);
s.pop();
int[] ret = new int[height()];
for (int j = 0; !s.isEmpty(); j++)
ret[j] = s.pop().x;
return ret;
}
示例10: getPathH
import edu.princeton.cs.algs4.Stack; //导入方法依赖的package包/类
private int[] getPathH(Pos T) {
Stack<Pos> s = new Stack<>();
for (Pos at = edgeTo[T.y][T.x]; at != null; at = edgeTo[at.y][at.x])
s.push(at);
if(!s.isEmpty())
s.pop();
int[] ret = new int[width()];
for (int j = 0; !s.isEmpty(); j++)
ret[j] = s.pop().y;
return ret;
}
示例11: invertQueue
import edu.princeton.cs.algs4.Stack; //导入方法依赖的package包/类
private static void invertQueue(Queue<String> queue) {
Stack<String> stack = new Stack<>();
while (!queue.isEmpty()) {
stack.push(queue.dequeue());
}
while(!stack.isEmpty()) {
queue.enqueue(stack.pop());
}
}
示例12: getInfixExpression
import edu.princeton.cs.algs4.Stack; //导入方法依赖的package包/类
private static String getInfixExpression(String input) {
Stack<String> operands = new Stack<>();
Stack<String> operators = new Stack<>();
String[] inputValues = input.split("\\s");
for (String value : inputValues) {
if (value.equals("(")){
//do nothing
} else if (value.equals("+")
|| value.equals("-")
|| value.equals("*")
|| value.equals("/")) {
operators.push(value);
} else if (value.equals(")")) {
String operator = operators.pop();
String value2 = operands.pop();
String value1 = operands.pop();
String subExpression = "( " + value1 + " " + operator + " " + value2 + " )";
operands.push(subExpression);
} else {
operands.push(value);
}
}
return operands.pop();
}
示例13: isBalanced
import edu.princeton.cs.algs4.Stack; //导入方法依赖的package包/类
private boolean isBalanced(String input) {
char[] parentheses = input.toCharArray();
Stack<Character> stack = new Stack<>();
for (char parenthesis : parentheses) {
if (parenthesis == '('
|| parenthesis == '['
|| parenthesis == '{'){
stack.push(parenthesis);
} else {
if (stack.isEmpty()) {
return false;
}
char firstItem = stack.pop();
if (parenthesis == ')' && firstItem != '('
|| parenthesis == ']' && firstItem != '['
|| parenthesis == '}' && firstItem != '{') {
return false;
}
}
}
return true;
}
示例14: decimalToBinary
import edu.princeton.cs.algs4.Stack; //导入方法依赖的package包/类
private static void decimalToBinary(int n) {
Stack<Integer> stack = new Stack<>();
while (n > 0) {
stack.push(n % 2);
n = n / 2;
}
for (int d : stack) {
StdOut.print(d);
}
StdOut.println();
}
示例15: infixToPostfix
import edu.princeton.cs.algs4.Stack; //导入方法依赖的package包/类
private static String infixToPostfix(String infixExpression) {
Stack<String> operands = new Stack<>();
Stack<String> operators = new Stack<>();
String[] inputValues = infixExpression.split("\\s");
for (String value : inputValues) {
if (value.equals("(")) {
//do nothing
} else if (value.equals("+")
|| value.equals("-")
|| value.equals("*")
|| value.equals("/")) {
operators.push(value);
} else if (value.equals(")")) {
String value2 = operands.pop();
String value1 = operands.pop();
String operator = operators.pop();
String newExpression = "( " + value1 + " " + value2 + " " + operator + " )";
operands.push(newExpression);
} else {
operands.push(value);
}
}
return operands.pop();
}