Здравейте,
Може ли някой да ми съдейства със задачата Array Search. Като цяло мисля, че решението трябва да е вярно, но тестовете ми дават Short circuit и Time Limit Exceeded …
import java.util.*;
public class Main {
private static final String COMMA_DELIMITER = ",";
public static void main(String[] args) {
int[] numbers = readIntArray();
StringBuilder stringBuilder = new StringBuilder();
for (int index = 1; index <= numbers.length; index++) {
if (numberIsNotPresent(index, numbers) && numberIsNotIncludedInStringBuilder(index,
stringBuilder)) {
stringBuilder.append(index).append(COMMA_DELIMITER);
}
}
printMissingNumber(stringBuilder);
}
private static int[] readIntArray() {
return Arrays
.stream(new Scanner(System.in).nextLine().split(COMMA_DELIMITER))
.mapToInt(Integer::parseInt)
.toArray();
}
private static boolean numberIsNotPresent(int queryNumber, int[] numbers) {
for (int index = 0; index < numbers.length; index++) {
if (numbers[index] == queryNumber) {
return false;
}
}
return true;
}
private static boolean numberIsNotIncludedInStringBuilder(int queryNumber,
StringBuilder stringBuilder) {
for (int index = 0; index < stringBuilder.length(); index++) {
if (stringBuilder.charAt(index) == queryNumber) {
return false;
}
}
return true;
}
private static void printMissingNumber(StringBuilder stringBuilder) {
if (stringBuilder.charAt(stringBuilder.length() - 1) == COMMA_DELIMITER.charAt(0)) {
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
}
System.out.println(stringBuilder.toString());
}
}