标签:info OLE lse length inf 技术 ima style solution
class Solution { public boolean isValid(String s) { if (s == null || s.length() == 0) return true; /** * 解法1: */ /*Stack<Character> stack = new Stack<>(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == ‘(‘ || c == ‘{‘ || c == ‘[‘) { stack.push(c); }else { if (stack.isEmpty()) { return false; } char temp = stack.pop(); if (c == ‘)‘ && temp != ‘(‘) return false; if (c == ‘}‘ && temp != ‘{‘) return false; if (c == ‘]‘ && temp != ‘[‘) return false; } } return stack.isEmpty();*/ /** * 解法2: */ Stack<Character> stack = new Stack<>(); for (char c : s.toCharArray()) { if (c == ‘(‘) { stack.push(‘)‘); }else if (c == ‘[‘) { stack.push(‘]‘); }else if (c == ‘{‘) { stack.push(‘}‘); }else if (stack.isEmpty() || c != stack.pop()) { return false; } } return stack.isEmpty(); } }
标签:info OLE lse length inf 技术 ima style solution
原文地址:https://www.cnblogs.com/HuangYJ/p/12819802.html