Skip to content

Instantly share code, notes, and snippets.

@karloku
Last active September 26, 2016 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karloku/ffed114f63f9e310d37b51ea6ecd8ca0 to your computer and use it in GitHub Desktop.
Save karloku/ffed114f63f9e310d37b51ea6ecd8ca0 to your computer and use it in GitHub Desktop.
括号匹配 Java Sample
import java.util.*;
public class Main{
public static boolean checkLegal(String A) {
Stack<Character> s = new Stack<>();
HashMap<Character,Character> map = new HashMap<Character,Character>();
map.put('[',']');
map.put('{','}');
map.put('(',')');
for (char c : A.toCharArray()) {
if (map.containsKey(c)) {
s.push(c);
}else if(map.containsValue(c)){
if(s.isEmpty())
return false;
char top = s.peek();
if(map.get(top).equals(c){
s.pop();
} else {
return false;
}
}
}
return s.isEmpty();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment