在hackerrank中获得相同测试用例的不同输出

时间:2017-08-06 13:34:36

标签: java algorithm data-structures stack

我正在hackerrank尝试平衡的paranthesis problem

我在相同的测试用例中获得了不同的输出,这非常奇怪!!

这是次测试案例:

()[{}()]([[][]()[[]]]{()})([]()){[]{}}{{}}{}(){([[{}([]{})]])}

如果我单独运行,我会得到正确答案(在我的情况下输出为"YES"

当这个测试用例与其他测试用例一起运行时,我得到"NO"作为输出。

这是我为5个hackos购买的实际测试用例:

21
()[{}()]([[][]()[[]]]{()})([]()){[]{}}{{}}{}(){([[{}([]{})]])}
{][({(}]][[[{}]][[[())}[)(]([[[)][[))[}[]][()}))](]){}}})}[{]{}{((}]}{{)[{[){{)[]]}))]()]})))[
[)](][[([]))[)
]}]){[{{){
{[(}{)]]){(}}(][{{)]{[(((}{}{)}[({[}[}((}{()}[]})]}]]))((]][[{{}[(}})[){()}}{(}{{({{}[[]})]{((]{[){[
()}}[(}])][{]{()([}[}{}[{[]{]](]][[))(()[}(}{[{}[[]([{](]{}{[){()[{[{}}{[{()(()({}([[}[}[{(]})
){[])[](){[)}[)]}]]){](]()]({{)(]])(]{(}(}{)}])){[{}((){[({(()[[}](]})]}({)}{)]{{{
[(})])}{}}]{({[]]]))]})]
[{
{}([{()[]{{}}}])({})
{({}{[({({})([[]])}({}))({})]})}
()[]
{)[])}]){){]}[(({[)[{{[((]{()[]}][([(]}{](])()(}{(]}{})[)))[](){({)][}()((
[][(([{}])){}]{}[()]{([[{[()]({}[])()()}[{}][]]])}
(}]}
(([{()}]))[({[{}{}[]]{}})]{((){}{()}){{}}}{}{{[{[][]([])}[()({}())()({[]}{{[[]]([])}})()]]}}
[(([){[](}){){]]}{}([](([[)}[)})[(()[]){})}}]][({[}])}{(({}}{{{{])({]]}[[{{(}}][{)([)]}}
()()[()([{[()][]{}(){()({[]}[(((){(())}))]()){}}}])]
({)}]}[}]{({))}{)]()(](])})][(]{}{({{}[]{][)){}{}))]()}((][{]{]{][{}[)}}{)()][{[{{[[
)}(()[])(}]{{{}[)([})]()}()]}(][}{){}}})}({](){([()({{(){{
}([]]][[){}}[[)}[(}(}]{(}[{}][{}](}]}))]{][[}(({(]}[]{[{){{(}}[){[][{[]{[}}[)]}}]{}}(}

我展示的子测试用例是这个大的第一个......

这是我的java代码:

package Java.Stacks.Hackerrank_Problems;

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

/**
 * Created by BK on 06-08-2017.
 */

public class BalancedParanthesis {
    public static void main(String... strings) {
        Scanner sc = new Scanner(System.in);
        int tc = sc.nextInt();
        for (int i = 0; i < tc; i++) {
            printAnswer(sc.next());
        }
    }

    private static void printAnswer(String input) {
        Stack<Character> stack = new Stack<>();
        boolean isValid=true;
        for (int i = 0; i < input.length(); i++) {
            char currentChar = input.charAt(i);
            if (currentChar == '{' || currentChar == '(' || currentChar == '[') stack.push(currentChar);
            else if (currentChar == '}') {
                if (stack.isEmpty())isValid=false;
                else {
                    if (stack.pop() != '{') {
                        isValid=false;
                    }
                }
            } else if (currentChar == ')') {
                if (stack.isEmpty())isValid=false;
                else {
                    if (stack.pop() != '(') {
                        isValid=false;
                    }
                }
            } else if (currentChar == ']') {
                if (stack.isEmpty())isValid=false;
                else {
                    if (stack.pop() != '[') {
                        isValid=false;
                    }
                }
            }
        }
        System.out.println(isValid?"YES":"NO");
    }
}

请帮我摆脱这个......谢谢你:)

这说它不起作用......

Error Image

2 个答案:

答案 0 :(得分:2)

你忘了检查堆栈是否为空,这是hackerrank接受的代码

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    static String isBalanced(String s) {
        Stack<Character> stack = new Stack<>();
        boolean isValid=true;
        for (int i = 0; i < s.length(); i++) {
            char currentChar = s.charAt(i);
            if (currentChar == '{' || currentChar == '(' || currentChar == '[') stack.push(currentChar);
            else if (currentChar == '}') {
                if (stack.isEmpty())isValid=false;
                else {
                    if (stack.pop() != '{') {
                        isValid=false;
                    }
                }
            } else if (currentChar == ')') {
                if (stack.isEmpty())isValid=false;
                else {
                    if (stack.pop() != '(') {
                        isValid=false;
                    }
                }
            } else if (currentChar == ']') {
                if (stack.isEmpty())isValid=false;
                else {
                    if (stack.pop() != '[') {
                        isValid=false;
                    }
                }
            }
        }
        return (isValid && stack.empty()?"YES":"NO");
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for(int a0 = 0; a0 < t; a0++){
            String s = in.next();
            String result = isBalanced(s);
            System.out.println(result);
        }
        in.close();
    }
}

注意小变化

  

(isValid&amp;&amp; stack.empty()?&#34; YES&#34;:&#34; NO&#34;);

答案 1 :(得分:1)

建议编辑。请试试。看看是否有效。

int tc = sc.nextInt();
String s=sc.nextLine();//Just for storing the escape character
    for (int i = 0; i < tc; i++) {
        printAnswer(sc.next());
    }