编程语言:Java
题目链接:https://leetcode-cn.com/problems/valid-parentheses/
题解:水题,这几天没时间,继续做水题
难度:easy
结果:
class Solution {
static String str = "()[]{}";
public boolean isValid(String s) {
Deque<Character> que = new LinkedList<>();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{') {
que.push(s.charAt(i));
} else {
if (que.isEmpty()||(str.indexOf(s.charAt(i)) != str.indexOf(que.poll()) + 1)) {
return false;
}
}
}
return que.isEmpty()?true:false;
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)