本文整理汇总了Java中edu.princeton.cs.algs4.Stack类的典型用法代码示例。如果您正苦于以下问题:Java Stack类的具体用法?Java Stack怎么用?Java Stack使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Stack类属于edu.princeton.cs.algs4包,在下文中一共展示了Stack类的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: 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);
}
示例6: 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;
}
示例7: 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);
}
}
示例8: getAllValidWords
import edu.princeton.cs.algs4.Stack; //导入依赖的package包/类
public Iterable<String> getAllValidWords(BoggleBoard board) {
this.board = board;
rows = board.rows();
cols = board.cols();
adj = (Bag<Integer>[]) new Bag[rows*cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int v = i*cols + j;
adj[v] = new Bag<Integer>();
if (checkWIsValid(i-1, j)) adj[v].add((i-1) * cols + j);
if (checkWIsValid(i+1, j)) adj[v].add((i+1) * cols + j);
if (checkWIsValid(i, j+1)) adj[v].add(i * cols + j+1);
if (checkWIsValid(i, j-1)) adj[v].add(i * cols + j-1);
if (checkWIsValid(i+1, j-1)) adj[v].add((i+1) * cols + j-1);
if (checkWIsValid(i+1, j+1)) adj[v].add((i+1) * cols + j+1);
if (checkWIsValid(i-1, j-1)) adj[v].add((i-1) * cols + j-1);
if (checkWIsValid(i-1, j+1)) adj[v].add((i-1) * cols + j+1);
}
}
validWords = new SET<String>();
for (int v = 0; v < rows*cols; v++) {
visitingDices = new Stack<Integer>();
marked = new boolean[rows*cols];
visitingDices.push(v);
marked[v] = true;
if(getLetterOnBoard(v) == 'Q')
searchValidWords(v, root.next['Q'-'A'].next['U' - 'A'], "QU", visitingDices);
else
searchValidWords(v, root.next[getLetterOnBoard(v)-'A'], getLetterOnBoard(v) + "", visitingDices);
}
return validWords;
}
示例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: checkAndSolveExpressionWaitingParenteshisResult
import edu.princeton.cs.algs4.Stack; //导入依赖的package包/类
private static void checkAndSolveExpressionWaitingParenteshisResult(Stack<String> operators, Stack<Double> values) {
String operatorBeforeExpression = operators.peek();
if (operatorBeforeExpression.equals("*")
|| operatorBeforeExpression.equals("/")
|| operatorBeforeExpression.equals("sqrt")) {
operators.pop();
solveExpressionOnTopOfStack(values, operatorBeforeExpression);
}
}
示例13: checkIfExistsAndSolveLowPrecedenceOperation
import edu.princeton.cs.algs4.Stack; //导入依赖的package包/类
private static void checkIfExistsAndSolveLowPrecedenceOperation(Stack<String> operators, Stack<Double> values) {
String operatorBeforeExpression = operators.peek();
if (operatorBeforeExpression.equals("-") || operatorBeforeExpression.equals("+")) {
operators.pop();
solveExpressionOnTopOfStack(values, operatorBeforeExpression);
}
}
示例14: 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();
}
示例15: 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;
}