• 项目中遇到的一些迭代解析问题


    1. // 迭代删除 ()...()...()

    def parseTail(sqlContext: String): String = {
    
      val pre = sqlContext.indexOf("(")
      val end = sqlContext.indexOf(")")
    
      if(pre == -1 && end == -1)
        return sqlContext
    
      parseTail(sqlContext.substring(0, pre) + sqlContext.substring(end + 1))
    }
    
    parseTail(sql)
    

      

    2. // 迭代删除 (..(..)..).. (..)  n重嵌套结构

    // 迭代计算 ( 个数
    def parseIndexNum(s: String): Int = {
      if (s.indexOf("(") > s.indexOf(")") || s.indexOf("(") == -1) return 0
      return parseIndexNum(s.substring(s.indexOf("(") + 1)) + 1
    }
    
    
    // 迭代删除 ()...()...()
    def parseTail(sqlContext: String): String = {
    
      val pre = sqlContext.indexOf("(")
      var end = sqlContext.indexOf(")")
    
      if (pre == -1 || end == -1)
        return sqlContext
    
      var sqlCopy = sqlContext
      val pre_count = parseIndexNum(sqlContext)
    
      if (pre_count > 1) {
        (0 until pre_count - 1).foreach(_ => {
          val index = sqlCopy.indexOf(")")
          sqlCopy = sqlCopy.substring(0, index) + "w" + sqlCopy.substring(index + 1)
        })
        end = sqlCopy.indexOf(")")
      }
      parseTail(sqlContext.substring(0, pre) + sqlContext.substring(end + 1))
    
    }
    

      

  • 相关阅读:
    poj3686 Windys
    poj3155 Hard Life
    luoguP2774 方格取数问题
    poj3469 Dual Core CPU
    poj3281 Dining
    luogu P3410 拍照
    离散化
    最短路(SPFA)
    lower_bound && upper_bound
    gcd
  • 原文地址:https://www.cnblogs.com/ruili07/p/10964401.html
Copyright © 2020-2023  润新知