9.6 print out all valid combinations of n-pairs of parentheses
来源:互联网 发布:数控车床梯形螺纹编程 编辑:程序博客网 时间:2024/04/28 21:07
leetcode 题目变种
package test;
import java.util.Stack;
public class isValidParenthesis {
private static boolean compareChar(char c1, char c2) {
switch (c1) {
case '(':
if (c2 != ')') {
return false;
} else
break;
case '[':
if (c2 != ']') {
return false;
} else
break;
case '{':
if (c2 != '}') {
return false;
} else
break;
}
return true;
}
private static boolean isValid(String s) {
int c = s.length();
Stack<Character> openS = new Stack<Character>();
if (c <= 1)
return false;
for (int i = 0; i < c; i++) {
char tmp = ' ';
// Character ch = s.charAt(i);
if ((s.charAt(i) == '(') || (s.charAt(i) == '[')
|| (s.charAt(i) == '{')) {
openS.push(s.charAt(i));
} else {
tmp = openS.peek();
if(openS.isEmpty() || !compareChar(openS.pop(), s.charAt(i)))
return false;
System.out.println(tmp+""+ s.charAt(i));
}
}
return openS.isEmpty();
}
public static void main(String[] args) {
String s = "({{{}}])";
// System.out.println(compareChar('(', ')'));
if (isValid(s))
System.out.printf(" isValid");
else
System.out.printf(" is not Valid");
}
}
结果: {}
{}
is not Valid
0 0
- 9.6 print out all valid combinations of n-pairs of parentheses
- Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthe
- Length of Valid Parentheses
- [LeetCode] Print All Combinations of a Number as a Sum of Candidate Numbers
- C++ 11 : create all possible k combinations of n items
- All out of love
- Go All Out Of It
- print all permutation of a string(recursion)
- Print all possible strings of length k that can be formed from a set of n characters
- Letter Combinations of a Phone Number,Swap Nodes in Pairs
- find out all attributes of an object
- LeetCode-Generate Parentheses & Letter Combinations of a Phone Number
- 给出一个set的字符和一个正数k,求所有由这个set能组成长度为k的字符串集合 print-all-combinations-of-given-length
- [Happy DSA] Print all interleavings of given two strings
- ypcat - print values of all keys in a NIS database
- 未完成--Sum of Pairs
- P26 (**) Generate the combinations of K distinct objects chosen from the N elements of a list.
- Fundamentals of Sound: How to Make Music out of Nothing at All
- freopen
- Jump Game - Leetcode
- 关于ADSL的学习,第一弹
- Word Ladder
- Word Ladder II
- 9.6 print out all valid combinations of n-pairs of parentheses
- JS中undefined、null以及NaN之间的区别,以及对象属性赋值的面试题
- Tripadvisor 面经解答 -持续更新ing
- Ubuntu14.04上Fig出现Couldn’t connect to Docker daemon …问题
- java Thread 学习
- Ubuntu 14.04 上 Fig 出现 client and server don’t have same version 问题及解决办法
- 【BZOJ 2819】 Nim
- BZOJ 1778 Usaco2010 Hol Dotp 驱逐猪猡 期望DP+高斯消元
- 【BZOJ 2597】 [Wc2007]剪刀石头布