Last active
September 26, 2016 07:03
-
-
Save karloku/ffed114f63f9e310d37b51ea6ecd8ca0 to your computer and use it in GitHub Desktop.
括号匹配 Java Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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