-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
93 additions
and
7 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,70 @@ | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class SomaDoPar { | ||
|
||
public static String encontrarParQuadratico(List<Integer> lista, int s) { | ||
for (int i = 0; i < lista.size(); i++) { | ||
for (int j = i; j < lista.size(); j++) { | ||
if (lista.get(i) + lista.get(j) == s) { | ||
return String.format("%d, %d", lista.get(i), lista.get(j)); | ||
} | ||
} | ||
} | ||
return "Não existe"; | ||
} | ||
|
||
public static String encontrarPar(List<Integer> lista, int s) { | ||
Set<Integer> conjunto = new HashSet<>(); | ||
for (int x : lista) { | ||
conjunto.add(x); | ||
int complemento = s - x; | ||
if (conjunto.contains(complemento)) { | ||
return String.format("%d, %d", x, complemento); | ||
} | ||
} | ||
return "Não existe"; | ||
} | ||
|
||
public static String formatarLista( | ||
List<Integer> lista, String separador) { | ||
|
||
// JEITO TERRÍVEL DE SE FAZER!!!! | ||
// String resultado = ""; | ||
// for (int i = 0; i < lista.size() - 1; i++) { | ||
// resultado += lista.get(i); | ||
// resultado += separador; | ||
// } | ||
// resultado += lista.get(lista.size() - 1); | ||
// return resultado; | ||
|
||
// JEITO CORRETO, usando um StringBuilder | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < lista.size() - 1; i++) { | ||
sb.append(lista.get(i)) | ||
.append(separador); | ||
} | ||
sb.append(lista.get(lista.size() - 1)); | ||
return sb.toString(); | ||
} | ||
|
||
public static void imprimirLista( | ||
List<Integer> lista, String separador) { | ||
System.out.println(formatarLista(lista, separador)); | ||
} | ||
|
||
public static void main(String[] args) { | ||
final int TAMANHO = 100_000; | ||
List<Integer> lista = new ArrayList<>(TAMANHO); | ||
for (int i = 1; i <= TAMANHO; i++) { | ||
lista.add(i); | ||
} | ||
long inicio = System.currentTimeMillis(); | ||
formatarLista(lista, "|"); | ||
long duracao = System.currentTimeMillis() - inicio; | ||
System.out.printf("\nTamanho = %d --- Duração = %d millis\n", | ||
TAMANHO, duracao); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class SomaDoParTest { | ||
|
||
@org.junit.Test | ||
public void encontrarPar() { | ||
List<Integer> lista = new ArrayList<>(); | ||
for (int x : new int[] {2, -10, 5, 32, 35, 51}) { | ||
lista.add(x); | ||
} | ||
|
||
String resultado = SomaDoPar.encontrarPar(lista, 25); | ||
assertTrue(resultado.equals("-10, 35") || resultado.equals("35, -10")); | ||
|
||
assertEquals("51, 51", SomaDoPar.encontrarPar(lista, 102)); | ||
assertEquals("Não existe", SomaDoPar.encontrarPar(lista, 6)); | ||
} | ||
} |