Java: Estruturas de Dados Rascunho
Resumo
Java
Java
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Stack;
import java.util.HashSet;
import java.util.Queue;
import java.util.TreeSet;
class Main {
public static void main(String[] args) {
System.out.println("Estruturas de Dados");
System.out.println("Array");
int[] prices = {22, 3, 19, 5, 0};
var result = maxProfit(prices);
System.out.println("price MAX: "+result);
System.out.println("Lista");
ArrayList list = new ArrayList<>();
list.add(10);
list.add(20);
System.out.println(list.get(1));
System.out.println("Lista Encadeada");
LinkedList llist = new LinkedList<>();
llist.addFirst(11);
llist.addLast(32);
System.out.println(llist.get(1));
System.out.println(llist.size());
System.out.println("Pilha LIFO");
Stack stack = new Stack<>();
stack.push(10);
stack.push(12);
var b = stack.pop();
System.out.println(stack.size() + " - stack: " + b);
System.out.println("Fila FIFO");
Queue queue = new LinkedList<>();
queue.add(10);
queue.add(12);
var a = queue.poll();
System.out.println(stack.size() + " - queue: " + a);
System.out.println("Set");
HashSet set = new HashSet<>();
set.add(10);
set.add(11);
if (set.contains(10)) {
for (Integer s : set) {
System.out.println(s);
}
}
System.out.println("Arvore");
TreeSet sett = new TreeSet<>();
sett.add(10);
sett.add(5);
System.out.println(sett.first());
}
public static int maxProfit(int[] prices) {
if (prices == null || prices.length <= 1) {
return 0;
}
int minPrice = prices[0];
int maxProfit = 0;
for (int i = 1; i < prices.length; i++) {
// Atualiza o preço mínimo de compra até o momento.
minPrice = Math.min(minPrice, prices[i]);
// Calcula o lucro potencial se vendermos hoje.
int potentialProfit = prices[i] - minPrice;
// Atualiza o lucro máximo se o lucro potencial for maior.
maxProfit = Math.max(maxProfit, potentialProfit);
}
return maxProfit;
}
Golang
package main
import (
"container/list"
"fmt"
)
func main() {
fmt.Println("Hello, 世界")
arr := [5]int{10, 20, 30, 40, 50}
slice := []int{}
slice = append(slice, 10, 20)
fmt.Println(arr[0])
fmt.Println(len(arr))
fmt.Println(slice[1])
fmt.Println(len(slice))
l := list.New()
e4 := l.PushBack(4)
e1 := l.PushFront(1)
l.InsertBefore(3, e4)
l.InsertAfter(2, e1)
// Iterate through list and print its contents.
for e := l.Front(); e != nil; e = e.Next() {
fmt.Println(e.Value)
}
stack := []int{}
stack = append(stack, 5) // push
stack = append(stack, 6) // push
stack = append(stack, 7) // push
stack = stack[:len(stack)-1] // pop
fmt.Println(len(stack))
}