• 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    미해결

디버깅 문의드립니다.

20.12.23 15:20 작성 조회수 114

1

안녕하세요 강의 보고 있습니다.

그런데 릿코드랑 이클립스에서 아래코드를 디버깅해봤는데, 릿코드에서는 정답이 나오는데 이클립스에서는 오답이 나옵니다.

코드에서 pop하는 연산이 없는 같은데 어떻게 정답처리(스택이 비게 되어 true리턴)가 되는지 궁금합니다.

감사합니다.

static Map<Character,Character> map = new HashMap<>();

static {

map.put('(', ')');

map.put('{', '}');

map.put('[', ']');

}

public static void main(String[] args) {

// TODO Auto-generated method stub

//String s = "([}}])";

//String s = "([])";

String s = "()";

System.out.println(isValid(s));

}

public static boolean isValid(String s) {

if(s.length()%2 != 0) return false;

System.out.println(s);

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

for(int i=0; i<s.length();i++) {

char c = s.charAt(i);

if(map.containsKey(c)) {

stack.push(map.get(c));

} else if(map.containsValue(c)) {

if(stack.isEmpty() || stack.peek() != c ) {

return false;

} else if(!stack.isEmpty() || stack.pop() == c) {return false;}

}

}

return stack.isEmpty();

}

답변 2

·

답변을 작성해보세요.

0

km54277님의 프로필

km54277

질문자

2020.12.27

else if(!stack.isEmpty() || stack.pop() == c) 으로 인해서 pop 연산이 실행되나보네요!

감사합니다~!

0

안녕하세요.

어떤 케이스를 넣으셨나요? 케이스를 디버깅해보시면 좋을거 같습니다.

그리고 pop은 밑에

else if(!stack.isEmpty() || stack.pop() == c) {return false;}  => 여기 pop 있네요

제 git에 map 예제가 있습니다.

비교해서 보시면 될거 같습니다.