java codility training Genomic-range-query

后端 未结 30 2860
悲哀的现实
悲哀的现实 2021-02-01 12:47

The task is:

A non-empty zero-indexed string S is given. String S consists of N characters from the set of upper-case English letters A, C, G, T.

<
30条回答
  •  爱一瞬间的悲伤
    2021-02-01 13:27

    scala solution 100/100

    import scala.annotation.switch
    import scala.collection.mutable
    
    object Solution {
      def solution(s: String, p: Array[Int], q: Array[Int]): Array[Int] = {
    
        val n = s.length
    
        def arr = mutable.ArrayBuffer.fill(n + 1)(0L)
    
        val a = arr
        val c = arr
        val g = arr
        val t = arr
    
        for (i <- 1 to n) {
          def inc(z: mutable.ArrayBuffer[Long]): Unit = z(i) = z(i - 1) + 1L
    
          def shift(z: mutable.ArrayBuffer[Long]): Unit = z(i) = z(i - 1)
    
          val char = s(i - 1)
          (char: @switch) match {
            case 'A' => inc(a); shift(c); shift(g); shift(t);
            case 'C' => shift(a); inc(c); shift(g); shift(t);
            case 'G' => shift(a); shift(c); inc(g); shift(t);
            case 'T' => shift(a); shift(c); shift(g); inc(t);
          }
        }
    
        val r = mutable.ArrayBuffer.fill(p.length)(0)
    
        for (i <- p.indices) {
          val start = p(i)
          val end = q(i) + 1
          r(i) =
            if (a(start) != a(end)) 1
            else if (c(start) != c(end)) 2
            else if (g(start) != g(end)) 3
            else if (t(start) != t(end)) 4
            else 0
        }
    
        r.toArray
      }
    }
    

提交回复
热议问题