Last active
July 5, 2016 05:41
-
-
Save lavcraft/8528536a3d3996f8f064198b9ebe9914 to your computer and use it in GitHub Desktop.
Напишите код, который для заданного диапазона чисел min..max
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
public class App { | |
public static void main(final String... args) { | |
final long begin = System.currentTimeMillis(); | |
int[] ints = IntStream.range(0, Integer.MAX_VALUE) | |
.parallel() | |
.filter(App::check) | |
.toArray(); | |
System.out.println("total numbers: " + ints.length + ", time: " + | |
(System.currentTimeMillis() - begin)/1000 + " s"); | |
} | |
private static boolean check(int n) { | |
final boolean[] digits = new boolean[10]; | |
while (n > 0) { | |
final int d = n % 10; | |
if (digits[d]) { | |
return false; | |
} else { | |
n = n / 10; | |
digits[d] = true; | |
} | |
} | |
return true; | |
} | |
} |
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
// Напишите код, который для заданного диапазона чисел min..max возвращает | |
// только те числа, в которых не повторяется ни одна цифра. Т.е. например, | |
// в диапазоне 98..103 возвращает числа (98, 102, 103) и не возвращает (99, 100, 101). | |
// Рассматриваются решения, оптимальные как по объему кода (т.е. наиболее краткие), | |
// так и оптимальные по времени выполнения. Для кратких решений разрешена реализация | |
// на языках, отличных от Java. Оптимальному по времени решению желательно за | |
// разумное время отрабатывать диапазон 0..2^31 | |
import java.util.HashSet; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.Set; | |
public class App { | |
public static void main(final String... args) { | |
final long begin = System.currentTimeMillis(); | |
final List<Integer> l = getNumbers(0, Integer.MAX_VALUE); | |
System.out.println("total numbers: " + l.size() + ", time: " + | |
(System.currentTimeMillis() - begin)/1000 + " s"); | |
} | |
private static boolean check(Integer n) { | |
final Set<Integer> set = new HashSet<>(); | |
while (n > 0) { | |
final Integer d = n % 10; | |
if (set.contains(d)) { | |
return false; | |
} else { | |
n = n / 10; | |
set.add(d); | |
} | |
} | |
return true; | |
} | |
private static List<Integer> getNumbers(final Integer min, | |
final Integer max) { | |
final List<Integer> r = new LinkedList<>(); | |
for (Integer n = min; n <= max; n++) { | |
if (check(n)) { | |
r.add(n); | |
} | |
} | |
return r; | |
} | |
} | |
// Use -XX:-UseGCOverheadLimit bro! |
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
import java.util.HashSet | |
import java.util.LinkedList | |
fun main(vararg args: String) { | |
val begin = System.currentTimeMillis() | |
val l = getNumbers(0, Int.MAX_VALUE) | |
println("numbers: ${l.size}, time: ${(System.currentTimeMillis() - begin)/1000} s") | |
} | |
fun check(number: Int) = run { | |
var n = number | |
val set = HashSet<Int>() | |
while (n > 0) { | |
val d = n % 10 | |
when (set.contains(d)) { | |
true -> return@run false | |
else -> { | |
n /= 10 | |
set.add(d) | |
} | |
} | |
} | |
true | |
} | |
fun getNumbers(min: Int, max: Int) = run { | |
val r = LinkedList<Int>() | |
for (n in min..max) { | |
if (check(n)) { | |
r.add(n) | |
} | |
} | |
r | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment