Rumah >Java >javaTutorial >Program Java untuk mencari elemen maksimum dan minimum dalam timbunan
Memahami Stack
di sini, kami menggunakan dua pembolehubah
min
max
<code class="language-java">import java.util.Stack; public class MaxMinInStack { public static void main(String[] args) { Stack<integer> stack = new Stack<>(); stack.push(10); stack.push(20); stack.push(30); stack.push(5); stack.push(15); int[] result = findMaxMin(stack); System.out.println("最大元素: " + result[0]); System.out.println("最小元素: " + result[1]); } public static int[] findMaxMin(Stack<integer> stack) { if (stack.isEmpty()) { throw new IllegalArgumentException("栈为空"); } int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; for (Integer element : stack) { if (element > max) { max = element; } if (element < min) { min = element; } } return new int[]{max, min}; } }</integer></integer></code>Unsur maksimum: 30 Elemen minimum: 5
di sini, kami melintasi timbunan dengan menggunakan operasi pop timbul dan mengemas kini nilai minimum dan maksimum seperti yang diperlukan. Tumpukan tambahan sementara menjimatkan unsur -unsur dan kemudian mengembalikan unsur -unsur ini ke timbunan asal.
output
<code class="language-java">import java.util.Stack; public class MaxMinInStack { public static void main(String[] args) { Stack<integer> stack = new Stack<>(); stack.push(10); stack.push(20); stack.push(30); stack.push(5); stack.push(15); int[] result = findMaxMinWithAuxiliaryStack(stack); System.out.println("最大元素: " + result[0]); System.out.println("最小元素: " + result[1]); } public static int[] findMaxMinWithAuxiliaryStack(Stack<integer> stack) { if (stack.isEmpty()) { throw new IllegalArgumentException("栈为空"); } Stack<integer> tempStack = new Stack<>(); int max = stack.peek(); int min = stack.peek(); while (!stack.isEmpty()) { int current = stack.pop(); if (current > max) { max = current; } if (current < min) { min = current; } tempStack.push(current); } while (!tempStack.isEmpty()) { stack.push(tempStack.pop()); } return new int[]{max, min}; } }</integer></integer></integer></code>Unsur maksimum: 30 Elemen minimum: 5
Kaedah ini menggunakan dua susunan tambahan, satu untuk mengingati elemen terbesar (
. maxStack
minStack
maxStack
minStack
<code class="language-java">import java.util.Stack; public class MaxMinInStack { // ... (main method remains the same) ... public static int[] findMaxMinWithTwoStacks(Stack<integer> stack) { Stack<integer> maxStack = new Stack<>(); Stack<integer> minStack = new Stack<>(); while (!stack.isEmpty()) { int current = stack.pop(); if (maxStack.isEmpty() || current >= maxStack.peek()) { maxStack.push(current); } if (minStack.isEmpty() || current <= minStack.peek()) { minStack.push(current); } } return new int[]{maxStack.peek(), minStack.peek()}; } }</integer></integer></integer></code>
output Unsur maksimum: 30 Elemen minimum: 5
<code class="language-java">import java.util.Stack; public class MaxMinInStack { static class StackNode { int value; int currentMax; int currentMin; StackNode(int value, int currentMax, int currentMin) { this.value = value; this.currentMax = currentMax; this.currentMin = currentMin; } } public static void main(String[] args) { Stack<stacknode> stack = new Stack<>(); push(stack, 10); push(stack, 20); push(stack, 30); push(stack, 5); push(stack, 15); int[] result = findMaxMinWithModifiedStack(stack); System.out.println("最大元素: " + result[0]); System.out.println("最小元素: " + result[1]); } public static void push(Stack<stacknode> stack, int value) { int max = stack.isEmpty() ? value : Math.max(value, stack.peek().currentMax); int min = stack.isEmpty() ? value : Math.min(value, stack.peek().currentMin); stack.push(new StackNode(value, max, min)); } public static int[] findMaxMinWithModifiedStack(Stack<stacknode> stack) { if (stack.isEmpty()) { throw new IllegalArgumentException("栈为空"); } StackNode topNode = stack.peek(); return new int[]{topNode.currentMax, topNode.currentMin}; } }</stacknode></stacknode></stacknode></code>
Setiap teknologi menyediakan cara khusus untuk menangani akses atau menyimpan item timbunan, yang menjadikannya sesuai untuk situasi tertentu berdasarkan batasan memori, keperluan prestasi, dan keperluan integriti data. Memahami dan memohon kaedah ini boleh membantu pemaju mengendalikan susunan dengan berkesan di Java, menjadikan aplikasi mereka paling sesuai untuk situasi tertentu.
Atas ialah kandungan terperinci Program Java untuk mencari elemen maksimum dan minimum dalam timbunan. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!