• 문제 링크
Valid Parentheses - LeetCode
Can you solve this real interview question? Valid Parentheses - Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: 1. Open brackets must be closed by the sam
leetcode.com
• 풀이 코드
public class Solution {
public boolean isValid(String s) {
char[] stk = new char[s.length() + 1];
int top = 1;
for (char c : s.toCharArray()) {
if (c == '(' || c == '{' || c == '[') stk[top++] = c;
else if (Math.abs(c - stk[--top]) > 2) return false;
}
return top == 1;
}
}'Problem Solving > LeetCode' 카테고리의 다른 글
| [LeetCode] 26. Remove Duplicates from Sorted Array - Java (0) | 2026.05.14 |
|---|---|
| [LeetCode] 21. Merge Two Sorted Lists - Java (0) | 2026.05.13 |
| [LeetCode] 14. Longest Common Prefix - Java (0) | 2026.05.11 |
| [LeetCode] 13. Roman to Integer - Java (0) | 2026.05.10 |
| [LeetCode] 12. Integer to Roman - Java (0) | 2026.05.09 |
댓글