scala 格雷码

def gray(n: Int): List[String] = {
    def go(x: Int, result: List[String]): List[String] = {
      if (x <= 0) result
      else {
        val cur = result.map(s => ("0" + s)) ++ result.reverse.map(s => ("1" + s))
        go(x - 1, cur)
      }
    }

    go(n, List(""))
  }

  

猜你喜欢

转载自www.cnblogs.com/wqkant/p/10588570.html