1 Star 0 Fork 0

表情扭曲 / leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lc20.java 1.46 KB
一键复制 编辑 原始数据 按行查看 历史
liu13 提交于 2019-03-08 22:08 . 20190308
package code;
import java.util.HashMap;
import java.util.Stack;
/*
* 20. Valid Parentheses
* 题意:括号匹配
* 难度:Easy
* 分类:String, Stack
*/
public class lc20 {
public static void main(String[] args) {
System.out.println(isValid("]"));
}
public static boolean isValid(String s) {
Stack<String> st = new Stack();
HashMap<String,String> hm = new HashMap();
hm.put("(",")");
hm.put("[","]");
hm.put("{","}");
for (int i = 0; i < s.length() ; i++) {
char ch = s.charAt(i);
if(ch=='(' || ch=='[' || ch=='{'){
st.push(String.valueOf(ch));
}else{
if(st.size()==0)
return false;
String temp1 = hm.get(st.pop());
String temp2 = String.valueOf(ch);
if(!temp1.equals(temp2))
return false;
}
}
if(st.size()==0)
return true;
return false;
}
public boolean isValid2(String s) {
Stack<Character> stack = new Stack<Character>();
for (char c : s.toCharArray()) {
if (c == '(')
stack.push(')');
else if (c == '{')
stack.push('}');
else if (c == '[')
stack.push(']');
else if (stack.isEmpty() || stack.pop() != c)
return false;
}
return stack.isEmpty();
}
}
1
https://gitee.com/abfantasy/leetcode.git
git@gitee.com:abfantasy/leetcode.git
abfantasy
leetcode
leetcode
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891