牛客网 2018校招真题 爱奇艺 括号匹配深度

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32767041/article/details/86516574

Description

牛客网 2018校招真题 括号匹配深度

Solving Ideas

缺失的括号 类似

Time complexity : O ( n ) O(n)
Space complexity : O ( 1 ) O(1)

Solution

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author wylu
 */
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        char[] str = br.readLine().toCharArray();

        int res = 0, depth = 0;
        for (char ch : str) {
            if (ch == '(') {
                depth++;
                res = Math.max(res, depth);
            } else {
                depth--;
            }
        }
        System.out.println(res);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_32767041/article/details/86516574
今日推荐