結果

問題 No.22 括弧の対応
ユーザー abcabc
提出日時 2016-10-18 13:01:08
言語 Java21
(openjdk 21)
結果
AC  
実行時間 131 ms / 5,000 ms
コード長 927 bytes
コンパイル時間 1,791 ms
コンパイル使用メモリ 71,944 KB
実行使用メモリ 56,516 KB
最終ジャッジ日時 2023-09-27 13:10:00
合計ジャッジ時間 4,921 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 111 ms
56,480 KB
testcase_01 AC 113 ms
55,848 KB
testcase_02 AC 115 ms
55,648 KB
testcase_03 AC 131 ms
56,332 KB
testcase_04 AC 119 ms
56,052 KB
testcase_05 AC 121 ms
56,080 KB
testcase_06 AC 122 ms
56,516 KB
testcase_07 AC 124 ms
56,024 KB
testcase_08 AC 126 ms
56,068 KB
testcase_09 AC 126 ms
56,036 KB
testcase_10 AC 120 ms
55,880 KB
testcase_11 AC 124 ms
56,252 KB
testcase_12 AC 123 ms
55,904 KB
testcase_13 AC 125 ms
55,724 KB
testcase_14 AC 114 ms
56,404 KB
testcase_15 AC 121 ms
56,408 KB
testcase_16 AC 123 ms
55,996 KB
testcase_17 AC 105 ms
56,240 KB
testcase_18 AC 113 ms
56,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
import java.util.Stack;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int numBrackets = sc.nextInt();
        int bracketIndexTofind = sc.nextInt();

        String s = sc.next();

        Stack<Integer> stack = new Stack<Integer>();

        for (int i = 0; i < numBrackets; i++) {

            char c = s.charAt(i);

            if (c == '(') {

                stack.add(i + 1);

            } else if (c == ')') {

                int openBracketIndex = stack.pop();

                if ((i + 1) == bracketIndexTofind) {

                    System.out.println(openBracketIndex);
                    return;

                }

                if (openBracketIndex == bracketIndexTofind) {

                    System.out.println(i + 1);
                    return;

                }

            }

        }

    }

}
0