Anyone know why my scala code did not work on this problem?


  • 0
    Z

    Anyone know why my scala code did not work on this problem? It worked all right on my laptop.

    import scala.collection.mutable.Stack
    def isValid(s: String): Boolean = {
      if (s=="") return true
      val stack = Stack[Char]()
      val slist = s.toList
      for (i <- slist) {
        i match {
          case '(' => stack.push(i)
          case '[' => stack.push(i)
          case '{' => stack.push(i)
          case ')' => if (stack.pop!='(') return false
          case ']' => if (stack.pop!='[') return false
          case '}' => if (stack.pop!='{') return false
          case _ => return false
        }
    
      }
      if (stack.isEmpty) true else false   
    }
    

  • 0
    F

    you have to pop the stack,when ')' matches '('


Log in to reply
 

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