Scala Exceeded Time Limit - Compile Time is huge in Scala


  • 0
    N

    This Scala Code takes more than 500ms for execution, I am not sure what optimization can be done

    object Solution {
      def convert(s: String, numRows: Int): String = {
    
        val len = s.length
        if (len > 0) {
    ""
        } else ""
      }
    
    }
    

    Also for review here is my complete code:

      def convert(s: String, numRows: Int): String = {
    
        val len = s.length
        if (len > 0) {
          val holder = new StringBuilder
          var zz = 0
          var jump = 0
          val str = s.toArray
          var i: Int = 0
          var zigZag: Int = numRows - 2
          for (j <- 0 to numRows - 1) {
            i = j
            if (zz == 0) {
              zz = zigZag
            }
            while (i < s.length) {
              holder.append(str(i))
              if (jump > 0 && jump <= numRows && jump + i < s.length) {
                holder.append(str(i + jump))
              }
    //          println(i + ":" + j + ":" + jump)
              i = i + numRows + zigZag
            }
            if (j < zigZag) {
              jump = numRows - (j + 2) + zz
            } else
              jump = 0
            zz = zz - 1
          }
          holder.toString
        } else ""
      }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.