n对括号的所有有效组合 [英] all valid combinations of n-pair of parenthesis

查看:96
本文介绍了n对括号的所有有效组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 我正在学习js ..

    • 我正在尝试编写一个简单的js程序..

    • 我要做的是打印n对
      括号的所有有效组合(正确打开和关闭)

    • 例如(),(()( )),(())

    • 我写的逻辑可以告诉我它是否正确

    • I am learning js now..
      • I am trying to write a simple js programme..
      • what I am trying to do is to print all valid combinations of n-pair of parenthesis(properly opened and closed)
      • eg (), (()()),(())
      • i have written the logic can you tell me whether its correct or not

      https://jsfiddle.net/e7mcp6xb/

      module.exports = Parentheses = (function() {
        var _isParenthesesMatch = function(str) {
          var parentheses = str.length;
          var rightParentheses = '(';
          var leftParentheses = ')';
          var rightCount = 0;
          var leftCount = 0;
      
          for(i=0;i<=str.length;i++){
             if(rightParentheses == str.charAt(i))
             {
                rightCount++;
             }
             else if(leftParentheses == str.charAt(i))
             {
                leftCount++;
             }
          }
      
          if(rightCount == leftCount){
            return true;
          }
          else(rightCount != leftCount){
            return false;
          }
      
        }
      
      }());
      


      推荐答案

      检查错误,但您可以轻松修复:在 for 循环的每一步中,左括号的数量不能小于结束数量:

      The check is wrong, but You can fix it easily: In each step of the for loop the number of opening parenthesis cannot be smaller than the number of closing ones:

      if (rightCount < leftCount)
          return false;
      

      整个函数应如下所示:

      function(str) {
          var rightParentheses = '(';
          var leftParentheses = ')';
          var rightCount = 0;
          var leftCount = 0;
      
          for (var i = 0; i <= str.length; i++) {
             if (rightParentheses == str.charAt(i))
                rightCount++;
             else if (leftParentheses == str.charAt(i))
                leftCount++;
      
             if (rightCount < leftCount)
                return false;
          }
      
          return rightCount == leftCount;
      }
      

      如果您想生成所有有效字符串,可以使用此功能:

      If You'd like to generate all valid strings, you can use this function:

      function nPair(n) {
          if (n == 0)
              return [""];
      
          var result = [];
          for (var i = 0; i < n; ++i) {
      
              var lefts = nPair(i);
              var rights = nPair(n - i - 1);
      
              for (var l = 0; l < lefts.length; ++l)
                  for (var r = 0; r < rights.length; ++r)
                      result.push("(" + lefts[l] + ")" + rights[r]);
          }
      
          return result;
      }
      
      // result of nPair(3):
      // ["()()()", "()(())", "(())()", "(()())", "((()))"]
      

      这篇关于n对括号的所有有效组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆