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 BigInteger multiply(BigInteger val) { | |
if (val.signum == 0 || signum == 0) | |
return ZERO; | |
int n = Math.max(bitLength(), val.bitLength()); | |
if (n > 1000) | |
return multiplyKaratsuba(val, n); | |
int[] result = multiplyToLen(mag, mag.length, |
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
object Permutation extends App { | |
def generate(s: String): Array[String] = s.length() match { | |
case 1 => Array(s) | |
case 2 => Array("" + s(0) + s(1), "" + s(1) + s(0)) | |
case _ => | |
val first = s.head | |
var perms = generate(s.tail) | |
var result: Array[String] = Array.fill(perms.length * (perms(0).length() + 1)) { "" } | |
var offset = 0 |
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
object Reverse extends App { | |
def reverse(l: List[Int]): List[Int] = l match { | |
case head :: Nil => head :: Nil | |
case head :: tail => reverse(tail) :+ head | |
} | |
assert { reverse(List(1, 2, 3, 4, 5)).corresponds(List(5, 4, 3, 2, 1)) {_ == _} } | |
} |
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
"0 1 2 3 4 5 6 7 8 9 10 11" | |
\/ \/ 8& 6& 8& 6& 1& 8& 1& 1& 8& 4& | |
[] [] [] [] 9& [] 9& [] [] | |
2& 1& 2& 6& [] 2& [] 1@ /\ | |
// -- [] [] -- // -- 1& | |
2& -- ** 0& 1& == | |
** 3& [] [] 2& | |
[] ** ** ?? | |
** 5& 7& |
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
object CF262B extends App { | |
import java.io.{PrintWriter} | |
import java.util.{Locale, Scanner} | |
val in = new Scanner(System.in) | |
val out = new PrintWriter(System.out) | |
def nextInt = in.nextInt | |
def nextDouble = in.nextDouble |
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 is part of Scalacaster project, https://github.com/vkostyukov/scalacaster | |
* and written by Vladimir Kostyukov, http://vkostyukov.ru | |
* | |
* Linked List http://en.wikipedia.org/wiki/Linked_list | |
* | |
* Prepend - O(1) | |
* Append - O(n) | |
* Head - O(1) | |
* Tail - O(1) |
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
def sort(s: Stack[Int]): Unit = { | |
def dump(a: Stack[Int], b: Stack[Int]): Unit = { | |
if (!b.empty) { | |
val v = b.pop | |
dump(a, b) | |
a.push(v) | |
} | |
} |
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 scala.collection.immutable.Queue | |
class Stack[+A](val in: Queue[A] = Queue.empty[A], val out: Queue[A] = Queue.empty[A]) { | |
def push[B >: A](v: B): Stack[B] = { | |
var i = in.enqueue(v) | |
var o = out | |
while (!o.isEmpty) { | |
val (vv, oo) = o.dequeue | |
o = oo |
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
object BSonLLBenchmark extends App { | |
class HeavyObject(val n: Int) extends Ordered[HeavyObject] { | |
def compare(that: HeavyObject): Int = { | |
var d = 0 | |
for (i <- 1 to 100000000) { | |
d += (math.abs(math.max(this.n, that.n) / math.min(this.n, that.n)) + 1) / i | |
} |
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 Test287 { | |
public static int binarysearch(int a[], int k) { | |
return binarysearch(a, k, 0, a.length); | |
} | |
public static int binarysearch(int a[], int k, int lo, int hi) { | |
if (lo == hi) return -1; | |
int p = (lo + hi) / 2; |
OlderNewer