原文:

给定一个长度为 n 的表达式 exp ,由一些括号组成。任务是在解析表达式时打印括号数字。 例:

input : (a (b*c)) (d/e)
output : 1 2 2 1 3 3
the highlighted brackets in the given expression
(a (b*c)) (d/e) has been assigned the numbers as:
1 2 2 1 3 3.
input : ((())(()))
output : 1 2 3 3 2 4 5 5 4 1 

来源:t2】flipkart 采访体验|第 49 集。

接近 :

  1. 定义一个变量 left_bnum = 1。
  2. 创建一个堆栈 right_bnum
  3. 现在,对于 i = 0 到 n-1。
    1. 如果 exp[i] == '('),则打印 left_bnum ,将 left_bnum 推到堆栈 right_bnum 上,最后将 left_bnum 增加 1。
    2. 否则如果 exp[i] == ')',则打印堆栈的顶部元素 right_bnum ,然后从堆栈中弹出顶部元素。

c

// c   implementation to print the bracket number
#include 
using namespace std;
// function to print the bracket number
void printbracketnumber(string exp, int n)
{
    // used to print the bracket number
    // for the left bracket
    int left_bnum = 1;
    // used to obtain the bracket number
    // for the right bracket
    stack right_bnum;
    // traverse the given expression 'exp'
    for (int i = 0; i < n; i  ) {
        // if current character is a left bracket
        if (exp[i] == '(') {
            // print 'left_bnum',
            cout << left_bnum << " ";
            // push 'left_bum' on to the stack 'right_bnum'
            right_bnum.push(left_bnum);
            // increment 'left_bnum' by 1
            left_bnum  ;
        }
        // else if current character is a right bracket
        else if(exp[i] == ')') {
            // print the top element of stack 'right_bnum'
            // it will be the right bracket number
            cout << right_bnum.top() << " ";
            // pop the top element from the stack
            right_bnum.pop();
        }
    }
}
// driver program to test above
int main()
{
    string exp = "(a (b*c)) (d/e)";
    int n = exp.size();
    printbracketnumber(exp, n);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java implementation to
// print the bracket number
import java.io.*;
import java.util.*;
class gfg
{
    // function to print
    // the bracket number
    static void printbracketnumber(string exp,
                                   int n)
    {
        // used to print the
        // bracket number for
        // the left bracket
        int left_bnum = 1;
        // used to obtain the
        // bracket number for
        // the right bracket
        stack right_bnum =
                   new stack();
        // traverse the given
        // expression 'exp'
        for (int i = 0; i < n; i  )
        {
            // if current character
            // is a left bracket
            if (exp.charat(i) == '(')
            {
                // print 'left_bnum',
                system.out.print(
                       left_bnum   " ");
                // push 'left_bum' on to
                // the stack 'right_bnum'
                right_bnum.push(left_bnum);
                // increment 'left_bnum' by 1
                left_bnum  ;
            }
            // else if current character
            // is a right bracket
            else if(exp.charat(i) == ')')
            {
                // print the top element
                // of stack 'right_bnum'
                // it will be the right
                // bracket number
                system.out.print(
                       right_bnum.peek()   " ");
                // pop the top element
                // from the stack
                right_bnum.pop();
            }
        }
    }
    // driver code
    public static void main(string args[])
    {
        string exp = "(a (b*c)) (d/e)";
        int n = exp.length();
        printbracketnumber(exp, n);
    }
}
// this code is contributed
// by manish shaw(manishshaw1)

python 3

# python3 implementation to print the bracket number
# function to print the bracket number
def printbracketnumber(exp, n):
    # used to print the bracket number
    # for the left bracket
    left_bnum = 1
    # used to obtain the bracket number
    # for the right bracket
    right_bnum = list()
    # traverse the given expression 'exp'
    for i in range(n):
        # if current character is a left bracket
        if exp[i] == '(':
            # print 'left_bnum',
            print(left_bnum, end = " ")
            # push 'left_bum' on to the stack 'right_bnum'
            right_bnum.append(left_bnum)
            # increment 'left_bnum' by 1
            left_bnum  = 1
        # else if current character is a right bracket
        elif exp[i] == ')':
            # print the top element of stack 'right_bnum'
            # it will be the right bracket number
            print(right_bnum[-1], end = " ")
            # pop the top element from the stack
            right_bnum.pop()
# driver code
if __name__ == "__main__":
    exp = "(a (b*c)) (d/e)"
    n = len(exp)
    printbracketnumber(exp, n)
# this code is contributed by
# sanjeev2552

c

// c# implementation to
// print the bracket number
using system;
using system.collections.generic;
class gfg
{
    // function to print
    // the bracket number
    static void printbracketnumber(string exp,
                                   int n)
    {
        // used to print the bracket
        // number for the left bracket
        int left_bnum = 1;
        // used to obtain the bracket 
        // number for the right bracket
        stack right_bnum = new stack();
        // traverse the given
        // expression 'exp'
        for (int i = 0; i < n; i  )
        {
            // if current character
            // is a left bracket
            if (exp[i] == '(')
            {
                // print 'left_bnum',
                console.write(left_bnum   " ");
                // push 'left_bum' on to
                // the stack 'right_bnum'
                right_bnum.push(left_bnum);
                // increment 'left_bnum' by 1
                left_bnum  ;
            }
            // else if current character
            // is a right bracket
            else if(exp[i] == ')')
            {
                // print the top element
                // of stack 'right_bnum'
                // it will be the right
                // bracket number
                console.write(right_bnum.peek()   " ");
                // pop the top element
                // from the stack
                right_bnum.pop();
            }
        }
    }
    // driver code
    static void main()
    {
        string exp = "(a (b*c)) (d/e)";
        int n = exp.length;
        printbracketnumber(exp, n);
    }
}
// this code is contributed
// by manish shaw(manishshaw1)

服务器端编程语言(professional hypertext preprocessor 的缩写)


java 描述语言


output: 

1 2 2 1 3 3

时间复杂度: o(n)。 辅助空间: o(n)。