给出多组只由如下字符组成的字符串“([{}])”,询问括号是否完全匹配 ‘(‘和’)’、'[‘和’]’、'{‘和’}’任务是匹配的,即不能为")("、"]["和"}{"

区块链毕设网qklbishe.com为您提供问题的解答

给出多组只由如下字符组成的字符串“([{}])”,询问括号是否完全匹配 ‘(‘和’)’、'[‘和’]’、'{‘和’}’任务是匹配的,即不能为")("、"]["和"}{"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
importjava.util.HashMap;
importjava.util.Map;
importjava.util.Scanner;
importjava.util.Stack;
 
publicclassMain {
    publicstaticvoidmain(String[] args) {
        Scanner sc = newScanner(System.in);
        intn = sc.nextInt();
        sc.nextLine();
        for(inti = 0; i < n; i++) {
            String s = sc.nextLine();
            isValid(s);
        }
    }
 
    privatestaticvoidisValid(String s) {
        if(s.length() <= 1) {
            System.out.println("no");
            return;
        }
        Map<Character, Character> map = newHashMap<>();
        map.put(‘(‘, ‘)’);
        map.put(‘[‘, ‘]’);
        map.put(‘{‘, ‘}’);
        Stack<Character> stack = newStack<>();
        for(inti = 0; i < s.length(); i++) {
            charitem = s.charAt(i);
            if(map.containsKey(item)) {
                stack.push(item);
            } else{
                if(!stack.isEmpty()) {
                    charleft = stack.pop();
                    charright = map.get(left);
                    if(right != item) {
                        System.out.println("no");
                        return;
                    }
                } else{
                    System.out.println("no");
                    return;
                }
            }
        }
        System.out.println("yes");
    }
}

45:50

以上就是关于问题给出多组只由如下字符组成的字符串“([{}])”,询问括号是否完全匹配 ‘(‘和’)’、'[‘和’]’、'{‘和’}’任务是匹配的,即不能为")("、"]["和"}{"的答案

欢迎关注区块链毕设网-
专业区块链毕业设计成品源码,定制。

区块链NFT链游项目方科学家脚本开发培训

承接区块链项目定制开发

微信:btc9767

QQ :1330797917

TELEGRAM: BTCOK9

承接区块链项目定制开发


qklbishe.com区块链毕设代做网专注|以太坊fabric-计算机|java|毕业设计|代做平台-javagopython毕设 » 给出多组只由如下字符组成的字符串“([{}])”,询问括号是否完全匹配 ‘(‘和’)’、'[‘和’]’、'{‘和’}’任务是匹配的,即不能为")("、"]["和"}{"