Scala solution with continuous Stream


  • 0
    D

    Idea is simple:

    • Have an infinite stream of integers that "zigzags" from 0 to num to 1 to num to 1 to num...
    • Zip them with each char in the string
    • Order the tuples based on the integer
    • Concat the list of chars
    def convert(s: String, numRows: Int): String = zigStream(numRows - 1).zip(s).sortBy(_._1).map(_._2).mkString
    def zigStream(zagLength: Int): Stream[Int] = Stream.continually((0 to zagLength) ++ (zagLength - 1 to 1 by -1)).flatten
    

    Something weird about the compiler and keeps saying Compile time exceeded with a base test case, so I'm inclined to believe there's something wrong with Leetcode.

    Solution is simple enough to test it out in REPL


Log in to reply
 

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